Competitive Programming Questions of Python |
In this article, we are going to write a Python function to check if an input number is prime or not.
If you are looking for a Python tutorial then it's here
def prime(n):
if n == 1: #1 is not a prime number
return False
for i in range(2,n):
if n%i == 0:
flag = False
break
else:
flag = True
return flag
def main():
n = int(input('Enter the number: '))
result = prime(n)
if result == True:
print(n, ' is a prime number')
else:
print(n, ' is not a prime number')
main()
The output of the code is:
Enter the number: 64
64 is not a prime number
Enter the number: 11
11 is a prime number
Comments
Post a Comment