10+ Python Prime Number Checker Program Examples

This post contains a total of 10+ Hand-Picked Python Prime number checker Program Examples with Source Code. All these prime number checker programs are made using Python Programming Language.

You can use the source code of these examples with credits to the original owner.

Related Posts

Python Prime Number Checker Programs

1. By David Ashton

Made by David Ashton. A simple Program to check if a Number is prime or not prime. Source

12
12 is not a prime number.
12 is divisible by 2, 3, 4, 6.
def is_prime(n):
    """Check for prime number."""
    if n > 2 and not n % 2:
        return False
    return all(n % i for i in range(2, int(n ** .5) + 1))

n = int(input() or "360")

print(f"{n} is{' ' if is_prime(n) else ' not '}a prime number.")

if not is_prime(n):
    divisors = (str(i) for i in range(2, n) if not n % i)
    print(f"{n} is divisible by {', '.join(divisors)}.")

2. By Kuba SiekierzyΕ„ski

Made by Kuba SiekierzyΕ„ski. The code checks if a given number is prime using RegEx matching. Source

Number to be checked: 10
10 is not a prime
import re
def is_prime(n):
    return not bool(re.match(r"^1?$|^(11+?)\1+$", "1"*n))

num = int(input("Number to be checked: \n"))
print (num, "is" + " not" * (not is_prime(num)) +" a prime")

3. By Brofar

Made by Brofar. Python Program to check if a number is prime or not. Source

This is the number you entered 4


4 is not prime
False
                   1  *  4
                   2  *  2
                   4  *  1



Neither number is emirp or a palindromic prime
import datetime
import time
a=int(input())
b = a
r = str(a)
s = r[::-1]
a = int(s)
print(f"This is the number you entered {r}\n")
if ((b%2) == 0):
   print(r + " is even")
else:
   print(r + " is odd")

m=int(b**(1/2))+1
for i in range(2,m):
    if b % i == 0:
        print(b, 'is not prime')
        p1 = False
        print(p1)
        break
else:
    print(b, 'is prime')
    p1 = True
    print(p1)
for i in range(1,30000):
    if b%i==0:
        print('                  ',i,' * ', b//i)
        
print()
if ((a%2) == 0):
   print(s + " is even")
else:
   print(s + " is odd")
   
n=int(a**(1/2))+1
for i in range(2,n):
    if a % i == 0:
        print(a, 'is not prime')
        p2 = False
        print(p2)
        break
else:
    print(a, 'is prime ')
    p2 = True
    print(p2)
for i in range(1,30000):
    if a%i==0:
        print('                  ',i,' * ', a//i)


if ((p1 == True) and (p2 == True) and (a > 10 ) and (b > 10)):
   print(f"\n\n{b} is prime, an emirp number and a palindromic prime")
else:
   print("\n\nNeither number is emirp or a palindromic prime")

4. By Goodmen

Made by Goodmen. The program checks if a number is prime or composite. Source

n =  3
V 3  =  1.7320508075688772
limit =  1
It's prime number
import math 
n = int(input())
if n < 2:
    print("A number must be more 1")
    quit()
elif n == 2:
    print("It's prime number")
    quit() 
i = 2
print ("n = ",n)
limit = int(math.sqrt(n))
print ("V",n," = ",math.sqrt(n))
print ("limit = ",limit) 
while i <= limit:
    print ("i =",i)
    print ("ost n / i =", n%i)
    if n % i == 0:
        print("It's composite number")
        quit() 
    i += 1 
print("It's prime number")

5. By Chandini

Made by Chandini. Basic Prime Checker Program. Source

2
Given number is a prime number
n=int(input())
i=1;count=0
while(i<=n):
    if(n%i==0):
        count+=1
        i+=1
    else:
        i+=1
if(count==2):
    print("Given number is a prime number")
else:
    print("Given number is not a prime number")

6. By ishwar

Made by ishwar. Source

54
54 is not a prime number
x,i = int(input()),2
if x==1 :
  print(x," is a neither prime number nor a composite number")
if x<0:
  print("sorry! there are no negative prime numbers exist")
while (x%i!=0 and x!=1 and x>=0 and x!=0) or x%i==0:
         if x==i and x%i==0 or x==(-i):
           print(x,"is a prime number")
         elif x%i==0 :
           print(x,"is not a prime number")
         if i==x or x%i==0:
          break
         i+=1

7. By Deepanshu Gupta

Made by Deepanshu Gupta. Python program that checks and outputs all the prime numbers in a given range. Source

Prime numbers between 1 and 10 are:
2
3
5
7
                         
lower = 1                       
upper = 10

print("Prime numbers between", lower, "and", upper, "are:")

for num in range(lower, upper + 1):
   # all prime numbers are greater than 1
   if num > 1:
       for i in range(2, num):
           if (num % i) == 0:
               break
       else:
           print(num)

8. By Alexander Gromozonov

Made by Alexander Gromozonov. Source

Enter your natural number:3
This is a prime number.
def isprime(n):
    if n==1:
        return 0
    for i in range(2,int(n**0.5)+1):
        if n%i==0:
            return 0
    return 1
  
while True:
    try:
        x=int(input("Enter your natural number: "))
    except ValueError:
        print("This is not a number! Try again.")
        continue
    if x>0: break
    else: print("Your number isn't natural! Try again.")
    
if isprime(x):
    print("This is a prime number.")
else:
    print("This is a composite number.")

9. By Divyanshu Singh

Made by Divyanshu Singh. A basic python program to check if the input number is prime number. Source

Enter number: 35

35 is not a prime number.
5 times 7 is 35
num=int(input("Enter number: "))
print(num)
print()
if num > 1:
   for i in range(2, num):
       if (num%i) == 0:
           print(num, "is not a prime number.")
           print(i, "times", num//i, "is", num)
           break
   else:
       print(num, "is a prime number.")
else:
   print(num, "is not a prime number.")

10. By misgav yosef

Made by misgav yosef. Source

your number is: 44
not prime
j=int(input("your number is: "))
list=[]
for i in range(j):
    list.append(i)
list.remove(list[0])
list.remove(list[0])
if all ([j%x!=0 for x in list]):
    print("{}\nprime".format(str(j)))
else:
    print("{}\nnot prime".format(str(j)))

11. By Stamen

Made by Stamen. Simple Prime Number Checker Program. Source

Type a number to check if it's prime: 22
2 times 11 is 22
22 is not a prime number.
from math import sqrt

try:

    num = int(input("Type a number to check if it's prime: \n"))
    x = 2
    y = "is a prime number."
    
    if num == 1:
        print(num, "is not a prime number.")
        exit(0)
    
    
    while x <= sqrt(num):
        if num % x == 0:
            y = "is not a prime number."
            print(x, "times", num//x, "is", num)
        x += 1
    print(num, y)
    
except ValueError:
    print("That isn't a number!")