prime number:-
A prime number (or a prime) is a natural number greater than 1 and that has no positive divisors other than 1 and itself.
Python function that takes a number as a parameter and check the number is prime or not
example 1:-
def test_prime(num):
if (num==1):
return False
elif (num==2):
return True;
else:
for x in range(2, num):
if(num % x==0):
return False
return True
print(test_prime(97))
output:-
True
>>>
example 2:-
# Python program to check if
# given number is prime or not
num = 11
# If given number is greater than 1
if num > 1:
# Iterate from 2 to n / 2
for i in range(2, int(num/2)+1):
# If num is divisible by any number between
# 2 and n / 2, it is not prime
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
output:-11 is a prime number
>>>
Post a Comment
If you have any doubts, Please let me know
Thanks!