Python Program to check whether the number is a PRIME Number or Not a PRIME Number
Write a python program that has function is_prime() that returns a 1 if the argument passed to it is a prime number and a 0 otherwise.
1. Add a defination named is_prime() which accepts one argument.
2. Start a Loop from 2 to a number divided by 2 and check whether the number is divisible by any other number apart from 1 and itself.
2.1 if divisible return 0, indicating the number is not a prime number.
2.2 else return 1, indicating a number is a prime number.
3. Read the number from the user and pass the number as an argument to the is_prime() function.
4. Check the return alue of the is_prime() method.
4.1 if the flag value is 0, then display the number is not a prime number.
4.2 if the flag value is 1, then display a number as a prime number.
Source code of Python Program to check whether the number is a PRIME Number or Not
def is_prime(x): i = 2; while(i <= x/2): if(x % i == 0): return 0 i = i + 1 return 1 num = int (input("Enter a number: ")) flag = is_prime(num) if (flag == 0): print(num, " is not a prime number") else: print(num, " is a prime number")
Enter a number: 111
111 is not a prime number
Enter a number: 10
10 is not a prime number
Enter a number: 17
17 is a prime number
If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.