Program to check prime number in python

Last updated 2 weeks ago | 39 views 75     5

Tags:- Python

Python | Check whether the given number is a prime number or not

This is an example of how to check whether the given number is a prime number or not. It takes an input number from the user to check the prime number

n = int(input('enter no.:'))
flag = True

if n > 2:
    for i in range(2, n):
        if n%i == 0:
            flag = False

    if flag == True:
        print(f'{n} is a prime number')
    else:
        print(f'{n} is not a prime number')
else:
    print('number must be > 2')
 

Output for the above code:

>>> 
====================== RESTART: E:\py\prime_no.py ======================
enter no.:5
5 is a prime number
>>> 
====================== RESTART: E:\py\prime_no.py ======================
enter no.:8
8 is not a prime number
>>> 
====================== RESTART: E:\py\prime_no.py ======================
enter no.:2
number must be > 2