WAP to input a number and check whether it is prime number or not in Python
Prime Number – a number that is divisible only by itself and 1. Example 3,5,7.etc. for this program it has to check every number up to n(The number to be checked) throughout using for loop iterative statement .
WAP the Prime Number Program using Python
The python code for the program to accept a number and produce out put is given below :-
Describe the Code
n=int(input(“Enter the number”)) #input of the number that needs to be checked whether it is prime or not
c=1 #creating of a variable c and initialization.
for i in range(2,n): #for loop and creating a variable i and variable going from intial 2 to n the number first input
if n%i==0: # this loop will go on to the last n to check whether the value n is divisible by i.
c=0 # if above condition becomes true then c value becomes zero
if c==1:If c remains 1 then the output is printed that this number is Number is prime
print(“Number is prime”)
print(“Number is not prime”) #If it comes as c not equal to 1 then it is printed Number is not prime.
Output of our Prime Number Program
Here the first input is 9 which is divisible by 1,3,9 so it does not fulfil the condition to be a prime number. The 3 is the extra number which loop finds and change the value of c=0 which in turn print that Number is not a prime .
then we type 2 which is divisible by 1,2 and so it is a prime number.
Download source code – click here
So, This is prime number program in python.
for more program- click here