Python Program to Check Prime Number

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 Program to Check Whether Number is Prime or Not


# 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