Computer Science, asked by pthakre288, 9 months ago

Wap to accept a number and check if it prime or not​

Answers

Answered by manavjaison
1

Heya friend,

Here we go :

Before we move on to the program, let's us see some points :

  • No negative numbers are treated as prime or composite
  • 0 is neither prime nor composite
  • 1 is also neither prime nor composite

So, we can check for the values greater than 1 and for the range we will not take the initial value as 1 , but 2, because each and every number is divisible by 1, so we will start loop from 2.

And we can use the break statement as let us say if we are checking 10 and we get on the first run that it divisible by 2, then we do not have to check for the values following it as it is clear that it is not prime.

Now,

Let's move on to the code:

                         SOURCE CODE

# program to check a prime number

n = int(input('Enter any number to check:'))

if n<=1:

   print('This number is neither prime nor composite')

else:

   for i in range (2,n):

       if n%i == 0:

           print('The number is not prime, it is composite')

           break

   else:

       print('The number is prime')

                              OUTPUT

Enter any number to check: 5

The number is prime

>>>  

>>>  

Enter any number to check: 8

The number is not prime, it is composite

>>>  

>>>  

Enter any number to check: 1

This number is neither prime nor composite

>>>

Thanks !

#BAL #answerwithquality

@MANAV

Answered by gaganadithyareddy9
0

Answer:

Hey! Here is your code in python...

x = int(input("Enter a number: "))

for i in range(2, x):

   if x%i == 0:

       print(x, "is not a prime number..")

       break

   else:

       print(x, "is a prime number..")

       break

# HOPE THIS HELPS!!

Similar questions