A prime number is a number greater than 1 that has
no divisors other than 1 and itself.
Examples:2, 3, 5, 7, 11, 13 are prime.
4, 6, 8, 9, 10 are not prime because they can be divided by numbers other than 1 and themselves.
Python Program
def is_prime_number(n):
# Prime numbers are greater than 1
if n <= 1:
return False
# Check divisibility from 2 to sqrt(n) -> (n**0.5)
for i in range(2, int(n**0.5) + 1):
if n % i == 0: # if divisible by i
return False
return True
# Test the function
num = int(input("Enter a number: "))
if is_prime_number(num):
print(num, "is a prime number")
else:
print(num, "is not a prime number")
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Prime Number Reminder:
2, 3, 5, 7, 11, 13are prime.4, 6, 8, 9, 10are not prime because they can be divided by numbers other than 1 and themselves.Python Program
Explanation
Base Case:
0,1, and negative numbers are not prime.Check divisibility:
√ninstead ofn-1.nhas a factor larger than√n, then the corresponding smaller factor would already have been found.Check modulus:
ndivides evenly byi, then it’s not prime.Return True if no divisors found:
Output :