Checking prime numbers is one of the most common questions asked in college practices so in this post, we will learn different ways to check prime number in Python with detailed explanations and examples.

But before we jump into the programming part let’s understand the basics first.

What is Prime Numbers?

A natural number that is only divisible by one and itself is known as a prime number. For example 2,3,5,7,11,13,17,19.. etc are prime numbers.

After understanding prime numbers, now let’s jump into the programming part and learn different methods to check prime numbers in Python.

Method-1: Check Prime Number Using For Loop

Before writing this program few programming concepts you have to Know:

Algorithm

  1. Take input From the User (num).
  2. Create one variable flag and initialize it with zero (flag = 0).
  3. Using for-loop iterate from 2 to num-1 (for i in range(2, num)).
  4. Inside for-loop check if num % i == 0 then do flag = 1 and break the loop.
  5. Outside of for-loop check if flag == 1 or num == 1 then print the given number is not prime else given number is prime.

Source Code

num = int(input("Enter a number: "))
flag = 0
for i in range(2,num):
    if num % i == 0:
        flag = 1
        break

if flag == 1 or num == 1:
    print(f"{num} is not prime number")
else:
    print(f"{num} is prime number")

Output

Enter a number: 5
5 is prime number

Method-2: Check Prime Number Using While Loop

Before writing this program few programming concepts you have to Know:

NOTE: In the case of a while-loop, we use the same algorithm as above. The only difference is that, instead of a for-loop, we use a while-loop.

Source Code

num = int(input("Enter a number: "))
flag = 0

i = 2  
while i < num:
    if num % i == 0:
        flag = 1
        break
    i+=1

if flag == 1 or num == 1:
    print(f"{num} is not prime number")
else:
    print(f"{num} is prime number")

Output

Enter a number: 9
9 is not prime number

Method-3: Check Prime Number Using Function

Before writing this program few programming concepts you have to Know:

Algorithm

  1. Define a function (def check_prime(n)).
  2. Check if n < 2 then return False (which means number is not prime).
  3. Outside of the if statement, using for-loop iterate from 2 to n-1
    (for i in range(2, n))
  4. Inside for loop check if n % i == 0 then return False.
  5. Outside of the for loop return True (which means the number is prime).

Source Code

def check_prime(n):
    if n < 2:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
    return True  # If no divisor is found, the number is prime  

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

if check_prime(num):
    print(f"{num} is Prime number")
else:
    print(f"{num} is not Prime number")

Output

Enter a number: 11
11 is Prime number

This is all about how we can check prime number in Python. I hope this article adds some value to your life – thank you.

Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment