Python Program to Check Prime Number

Here you will get python program to check prime number.

A number is called prime number if it is only divisible by 1 or itself. For example 2, 3, 5, 7, 11, etc are prime numbers.

Below program takes a number as input and checks it is prime number or not.

Python Program to Check Prime Number

num = int(input("enter a number: "))

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

Output

enter a number: 11
prime number

Video Tutorial


Comment below if you have any queries related to above python prime number program.

27 thoughts on “Python Program to Check Prime Number”

  1. Here according to the program else is inside the for loop. When the if condition fails, directly it will print as the number is not prime until checking up to the last value. If I take num=9, then in first iteration (9%2)==0 (false) else statement will be executed. But 9 is not prime right. can u explain me this prgm.

    1. See, this else is not the part of if. In python for loop is little different from other languages like C and C++. In python we can use a else statement with for loop. After the loop ends, else statement is executed. In case break is used somewhere in loop then else is not executed. The program is correct and works fine, just try to run it.

      You can learn about for loop in python here https://www.programiz.com/python-programming/for-loop

      1. You are foolish , just go and run it you will come to know that its correct and if it would be wrong too just have etiquettes to say it wrong . This is not the way to abuse anyone , any human can commit mistakes and after all , he is only helping us . JUST BEHAVE PROPERLY.

    2. if we are checking with the number 2, this condition becomes true ( if num % i == 0:)
      2%2==0. Then why it is going to the else statement?

  2. def prime_check(number= ‘Plese enter a valid number’):
    for s in range(2,number+1):
    if number<=1 or number / s != 0:

    print("it's not a prime number")
    else:

    print("It's a prime number")
    return

  3. Rahul Deshmukh

    Sir I can check as below also.
    I am mentioning the logic here only
    initialize i to 2
    Store the number to be checked at n
    now I will check for every i that whether it divides n or not;and the loop will continue till i reaches n
    if for any value of i it divides n,n will be non-prime and if no value of i can divide n then n will be prime.
    Sir I cannot fully apply the idea in my program.Will you please help me ASAP?

  4. sir ur program gives an error while we entering greater than 10 .this error only occur when we enter prime number ..

  5. thanks brother , my program doesn’t runs because i doesn’t put the break statement at end of loop.

    for i in range(2,30):
    j=2
    while j<i:
    if i%j==0:
    j=j+1
    break
    else:
    print("%s it is prime"%(i))
    j=j+1
    break

  6. def primes(num):
    list=[]
    for i in range (2,int(num/2)+1):
    if ((num%i)==0):
    break
    else:
    list.append(i)
    return list
    primes(11)
    iam unable to append primes to list please help me out

    1. Birendra Budhathoki

      It returns a list of primes upto a number n.

      def primes_upto(n):
      arr = []
      for i in range(1, n+1):
      if i in [2, 3]:
      arr.append(i)
      continue
      for j in range (2, (i//2) + 1):
      if i % j == 0:
      break
      if j == i//2:
      arr.append(i)
      return arr

  7. Birendra Budhathoki

    This program has a small error. This works fine for other inputs except for 4. It says 4 is a prime number which is actually not.

    When input is 4, range function is range(2, 2). This returns an empty list. Thus the execution is jumped to else block. And, it prints ‘prime number’.

    Hence, it should be range(2, int(n/2) + 1) instead of range(2, int(num/2)).
    This corrects the error!

    1. Bhartendu Kumar

      As the function range(2,var) will produce list of numbers from 2 up to var.i.e. 1 less than var. so in the case var=2,
      range(2,2) produces EMPTY LIST. And thus 2 never gets break statement.

  8. I want given number is palindrome or not program by using if conditions only don’t use while condition to write that program on python

  9. #this code prints prime number according to your limit
    def primenumber(limit):
    for j in range (2,limit):
    for i in range(2,j):
    if j%i == 0 :
    break
    else:
    print(j)
    x=int(input(“enter the limit: “))
    primenumber(x)

  10. # taking input from user
    number = int(input(“Enter any number: “))

    # prime number is always greater than 1
    if number > 1:
    for i in range(2, number):
    if (number % i) == 0:
    print(number, “is not a prime number”)
    break
    else:
    print(number, “is a prime number”)

    # if the entered number is less than or equal to 1
    # then it is not prime number
    else:
    print(number, “is not a prime number”)

  11. import math
    a = int(input(‘enter a positive number’))

    def checkprime(num):
    for i in range(2,int(math.sqrt(num))):
    if(num % i == 0):
    print(“not prime”)
    break
    else:
    print(“prime number”)
    return “prime”

    b = checkprime(a)

  12. Typing mistake(excluded some lines in the previous one)
    This is the corrected one:

    x=eval(input (“enter number: “))
    a=[]
    for i in range(1,x+1):
    if x%i==0:
    a.append(i)
    if x in range(2,x+1) and len(a)=2:
    print(“The number is composite.”)
    elif x in range(0,2):
    print(x,”is neither prime nor composite.”)

  13. x=eval(input (“enter number: “))
    a=[]
    for i in range(1,x+1):
    if x%i==0:
    a.append(i)
    if x in range(2,x+1) and len(a)=2:
    print(“The number is composite.”)
    elif x in range(0,2):
    print(x,”is neither prime nor composite.”)

    #This is my unique self designed code which is fully functional.

Leave a Comment

Your email address will not be published. Required fields are marked *