codingstreets
Search
Close this search box.

Python Program: Check Prime Number in Python

python-program-check-prime-number-in-python
Photo by Magda Ehlers on Pexels.com

Discover the power of prime numbers in Python! Learn how to efficiently identify and generate prime numbers using Python programming. Master the fundamental algorithms and explore practical applications. Unleash the potential of prime numbers with our comprehensive Python project.

Let’s check out another Python project.

Table of Contents

Prime Number

A number with only two factors 1 and the number itself. 

In other words, a number is divisible by only 1 and the number itself.

E.g., – 2, 3, 5, 7, 11,…. 

Note: Smallest Prime number is 2.

Using Conditional Statement 

Example: Check the Prime number using a conditional statement.

				
					num = 4
if num <= 1:
    print(num, "is not Prime Number.")
else:
    for number in range(2, num):
        if num % number == 0:
            print(num, "is not Prime Number.")
            break
    else:
        print(num, "is Prime Number.")
				
			

Explanation: 

The code starts with the variable “num” which stores the value to be checked for the Prime number.

The condition if num <= 1 is used to ensure that if a given number <= 1, then it is not a Prime number.

Now, the else condition along with for loop is used to check if the given number is a Prime number or not.

for loop iterates over each number sequentially, starting from range 2 to num and stored in the variable “number”.

The condition num % number == 0 checks, if the given number is divisible by each number, iterated stored in the variable “number” meaning that the condition is True.

The print() function displays the message that the given number is not a Prime number.

The break keyword is used to stop the command once the condition num % number == 0 is True.

If don’t use the break keyword, the print() function is displayed for each time the condition num % number == 0 is checked. 

If the condition num % number == 0 is False, else statement is defined that uses the print() function to display the given number as a Prime number. 

Using Flag Variable 

Example: Check the Prime number using the flag variable.

				
					num = 30
is_prime = True

if num <= 1:
    is_prime = False
else:
    for number in range(2, num):
        if num % number == 0:
            is_prime = False
            break

if is_prime:
    print(num, "is Prime Number.")

else:
    print(num, "is not Prime Number.")
				
			

Explanation: 

The explanation of this example is as same as above except few points. 

The code uses a flag variable as is_prime holds Boolean values i.e., True and False.

If the condition num <= 1 is True, means the given number is not a Prime number since variable is_prime holds False.

If the condition num <= 1 is False, which means the given number > 1, then we use the else condition along with for loop. 

for loop iterate each number within the range 2 to num-1 and stored it in the variable “number”.  

If the condition num % number == 0 is True, means the given number is not a Prime number because variable is_prime holds False.

The break keyword is used to stop the command once the condition num % number == 0 is True.

Now, we use the flag variable is_prime as a reference in the conditional statement to define whether the given number is a Prime number or not. 

If is_prime is True, means if the condition is True, otherwise else condition is True.

Using while loop

Example: Check the Prime number using a while loop.

				
					num = 4
is_prime = True
i = 2

while i < num:
    if num % i == 0:
        is_prime = False
        break
    i += 1

if is_prime:
    print(num, "is a prime number.")
else:
    print(num, "is not a prime number.")
				
			

Explanation: 

The explanation of this example is as same as above except few points. 

The code starts by checking the condition i < num, if it is True the condition num % i == 0 is checked if it is divisible then the “num” is not a Prime number, and the command is stopped using keyword break

In each loop, (i += 1) i value is incremented by 1.

Once the loop is terminated, meaning while the loop condition is False, the command comes to the conditional statement and uses is_prime as a reference to check whether the given number is Prime or not. 

Using def function

Example: Check the Prime number using the def function. 

				
					def is_prime(num):
    if num <= 1:
        return False
    else:
        for number in range(2, num):
            if num % number == 0:
                return False
        return True


num = 17
if is_prime(num):
    print(num, "is a prime number.")
else:
    print(num, "is not a prime number.")
				
			

Explanation:

The code starts by taking the number defined as the parameter “num” passed to function is_prime.

If the condition num <= 1, is False returns False meaning that the given number <=1. 

Now,  for loop iterate each number within the range 2 to num-1 and stored it in the variable “number”.  

If the condition num % number == 0 is True, means the given number is not a Prime number and returns False.

Once the for loop is terminated, it returns True, meaning that the given number is a Prime number.

Finally, the conditional statement is checked now and displays a message using the print() function based on the condition.

Conclusion

The article provides an overview of how to determine whether a given number is prime using the Python programming language. It presents a step-by-step approach and provides a code snippet to implement the prime number-checking algorithm.

Recent Articles