Python Program to Check Prime Number

This python program checks whether a given number by user is prime or not. A Prime Number is a positive integer greater than 1 which is divisible by 1 and itself.

In other words, Prime number is a whole number greater than 1 than whose factors are 1 and itself.

Prime Number Examples: 2, 3, 5, 7, 11, 13, ...

Also try: Check Prime Number Online & Generate Prime Numbers Online

In this python example, we first read number from user using built-in function input(). Since function input() returns string value, we need to convert given number to number type using int(). And then given number is checked for PRIME or NOT PRIME.

Python Source Code: Check Prime


# Prime check

# Reading number from user
number = int(input('Enter number: '))

flag = 1

for i in range(2,int(number/2)):
    if number%i==0:
        flag=0
        break

if flag==1 and number>=2:
    print('PRIME')
else:
    print('NOT PRIME')

Output

Run 1:
-----------------
Enter number: 7
PRIME

Run 2:
-----------------
Enter number: 237
NOT PRIME

Run 3:
-----------------
Enter number: -37
NOT PRIME