Given an integer,Write a python program to check if an integer is a prime number or not.
Prime Number
A given a number is said to be prime if the number has exactly two factor which is 1 and itself or the number is said to be prime if the number is divided by 1 and itself.
example -2 = factor of 2 is 1 and 2 so it is a prime because it has exactly two factor
Example:
1. 2 -prime
2. 1 - Not a prime
3. 3 -Prime
Python Program
def prime(n):
#To count the factor of a number
count = 0
#we check from 2 because 1 is not a prime number
if n > 1:
for i in range(1,n+1):
if n%i==0:
count+=1
else:
return "Not a prime"
#If the number has exactly 2 factor then we return as a prime number
if count==2:
return "Prime"
else:
return "Not a prime"
#Taking user input
n = int(input())
#calling the prime number function
print(prime(n))
Input:
2
Output:
Prime
Input:
1
Output:
Not a prime
Comments
Post a Comment