Basic Programs

If Else Programs

While loop Programs

For loop Programs

List Programs


Python Program to Check given Number is Prime or Not


In this example shows, how to check given number is prime number or not.

Steps
  1. Get the input from user.
  2. A basic way to find if a number is prime is to loop from 2 to n-1 .
  3. Check if the number is divisible by any number in this range. If it is, then it's not a prime number
Example:
Example.py
a = 0
n = int(input('Enter the number to check Prime or Not : '))
i = 2
while i <= (n/2):
    if (n%i) == 0:
        a = 1
        break
    else:
        i += 1
    
if n == 1:
    print('1 is neither')
elif a == 0:
    print(n,' is a prime number.')
elif a == 1:
    print(n,' is not a prime number.')

Output 1

Enter the number to check Prime or Not : 7
7 is a prime number.

Output 2

Enter the number to check Prime or Not : 16
16 is Not a prime number.

Output 3

Enter the number to check Prime or Not : 1
1 is neither

Prev Next