Program to check if the given number is Prime or not in Python

Program to check if the given number is Prime or not in Python

Prime or not in Python

In this article we will see a program to find whether a number is Prime or not in Python. A number is said to be a prime number if and only if it is only divisible by 1 or itself (factors==>1,number itself) . If a number has more than two factors than the number is said to be not a prime number.
  • Sample input: 17
  • Sample output: The given number is prime
See the two different approaches below , solution 1 is more efficient than solution 2

Method 1

Algorithm:

  • Step 1: Initialize the variable a and count to zero
  • Step 2: Read the input
  • Step 3: Set a to n//2 (// in python implies integer division) , we can make a to square root of number to make it more efficient
  • Step 4: iterate through 2 to a+1 and check whether it is divisible by i or not
  • Step 5 : if divisible , then print “not a prime number” and set count to 1 and break from the loop
                  else continue iteration
  • Step 6 : if count is still zero then the given number is prime number
prime or not in python

Python code:

a = 0
count = 0
n=int(input("Enter the number to check if it is prime or not: "))
a = n // 2;

for i in range(2,a+1):
    if (n % i == 0):
        print("The given number is not prime")
        count = 1
        break
if (count == 0):
    print("The given number is prime")
Enter the number to check if it is prime or not: 17
The given number is prime

Method 2

Algorithm

  • Step 1: Take an empty array
  • Step 2: Read the number
  • Step 3: Iterate a  from 1 to the number
  • Step 4: If number is divisible by iterator i then append the number to factors array
  • Step 5: if length of factors array is 2 then given number is prime
                 else the given number is not prime number
    (for more clarity factors of numbers also printed)

Python Code:

factors=[ ]
n=int(input(“Enter the number to check if it is prime or not: “))
for i in range(1,n+1):
    if (n % i == 0):
        factors.append(i)
if (len(factors)==2):
    print(“The given number is prime”)
else:
    print(“The given number is not prime number”)
print(“Factors of given number is”,factors)
Enter the number to check if it is prime or not: 16
The given number is not prime number
[1, 2, 4, 8, 16]

21 comments on “Program to check if the given number is Prime or not in Python”


  • Mahima

    def prime(n):
    if n==1:
    return False

    else:

    for i in range(2,int(n**(1/2)+1)):
    if(n%i==0):
    return False

    return True

    n = int(input(“Enter the integer number:”))
    if(prime(n)):
    print(“The number is a Prime”)
    else:
    print(“The number is not a prime”)


  • Iqra

    Let’s Try it!
    n=int(input(“ENTER the number for check prime or not:”))
    for i in range(2,n):
    if(n % i ==0):
    print (“Given number is not prime”,end=” “)
    break
    else:
    print(“Given number is PRIME”)


  • rajat

    More easy solution for the Solution 1:
    x = int(input(“Enter the number:”))
    for i in range(2, x):
    if x % i == 0:
    print(“Not Prime”)
    break
    else:
    print(“Prime”)


  • VIKRANT

    a=int(input())
    b=2
    c=3
    d=5
    e=7
    if ((a==b)or(a==c)or(a==d)or(a==e)):
    print(“the number is a prime number”)
    elif ((a%b==0)or(a%c==0)or(a%d==0)or(a%e==0)):
    print(“number is not a prime number”)
    else:
    print(“the number is a prime number”)


  • RishiRanjan

    n=int(input(“Enter a Number for finding PrimeNumber:-“))
    if n<2:
    print(n, "is not a prime number")
    else:
    for i in range(2,n):
    if(n%i==0):
    print(n, " is not a prime number")
    break

    else:# this for part else not of if part because if part break

    print(n, " is prime number")


  • Shubham

    num=int(input())
    if num%2==0 and num!=2:
    print(“not prime”)
    else:
    print(“prime”)


    • Shubham

      num=int(input(“Enter the value:”))
      prime=True
      for i in range(2,num):
      if num%i==0 and num!=2:
      prime=False
      break
      if prime:
      print(“This no. is prime”)
      else:
      print(“This no. is not prime”)


  • gaurav

    n = int(input(“Enter num: “))

    i = 2
    while i < n:
    if (n%i) == 0:
    print("not a prime number")
    exit()
    i+= 1
    print("prime number")


  • pavan

    easy one
    x=int(input(“Enter the number:”))
    for i in range(2,x):
    if(x%2==0):
    print(” not a prime”)
    break
    else:
    print(“prime”)


  • Aruna

    num = int(input( “Enter a number”))
    if num >1:

    for i in range (2, num):
    if (num % i) == 0:
    print (” its not a prime number”)
    break
    else:
    print (“its a prime number”)

    else:
    print(“its not a prime nor composite number”)


  • Sourabh

    another approach:
    n = int(input(‘enter the num’))
    for i in range(2, n+1):
    if n % i == 0:
    break
    if n == i:
    print(n, ‘is prime’)
    else:
    print(n,’is not prime’)


  • Rohit

    n=5
    c=0
    for i in range (1,n+1):
    if (n%i==0):
    c=c+1
    if c==2:
    Print (n,” is leap year”)
    Else :
    Print (n,” is not a prim”)


  • Rohit

    n= int(input(“enter a no.”))
    i=1
    c=0
    while i<=n:
    if n%i==0:
    c=c+1
    i=i+1
    if c==2:
    Print(n,"is prime no.")
    else:
    Print(n," is not a prime")