# Python Program to Check Prime Number

Updated Apr 08, 2024

## What is Prime Number?

A positive natural number greater than 1, which only divisible by itself and 1 is known as a prime number.

For example, 23 is a prime number because it is only divisible by 1 and itself whereas 24 is not a prime number because it is divisible by 1,2,3,4,6,8,12 and itself.

In this tutorial, you will learn how to write a python program to check whether a number is a prime number or not.

This program requires the knowledge of the following python functions:

• If and else statement
• For loop
• Python break and continue
• Modulus operator(%)

## Python Program to Check Prime Number

### Approach of Program

• The first thing you need to check in the program is that the input variable must be greater than 1 as mentioned above prime number is greater than 1.
• The second thing you need to check is if the input num is exactly divisible by any number from 2 to num - 1. If in case you find a factor in that range, then the number is not prime or else the number is prime.

## 1) Check Prime Number Using For Loop

``````# Program to check if a number is prime or not
# Input from the user
num = int(input("Enter a number: "))

# If number is greater than 1
if num > 1:
# Check if factor exist
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
break
else:
print(num,"is a prime number")

# Else if the input number is less than or equal to 1
else:
print(num,"is not a prime number")``````

OUTPUT:

In this program, we have utilised a for loop to check if the number is prime.

This is achieved by the code through the following steps:

Step 1: Receive the input variable, which is “num” in this case.

Note: You can use either a predefined variable or a user-defined one.

Step 2: Check if the number is greater than 1. If not, then the else condition is summoned, and the interpreter prints the number to be prime.(All negative integers, 0 and 1 are not prime and are less than one.)

Explanation:

All negative integers, 0 and 1, that are less than 1,  are not prime.

Step 3: If the num is greater than 1 however, then the ‘if’ condition is executed, which contains the ‘for’ loop. The ‘for’ loop checks if the number is divisible by any number between 2 and num/2 + 1.

Explanation:

It is done by taking all the numbers between 2 and num/2 + 1 and checking if they leave any remainder. This is done by using the modulus operator (%). The modulus operator is used to check if the remainder is equal to 0.

Step 4: If the remainder is equal to 0, then the if condition is called and the interpreter outputs that the number is not prime.
This means that the number does have factors other than 1 and the number itself. Then,  a break command is executed to end the code right then and there.

Step 5: However, if the else condition is called, it means that the number does not have any other factors, and it is a prime number.

To Avoid:

• Not using a break command:
This will result in the code also executing the else statement that is outside the first if statement.
• Not converting the num variable into the integer type (int(num = 24)):
This will result in a data type error.

## 2) Check Prime Number Using While Loop

``````# Program to check if a number is prime or not

# Input from the user
num = int(input('Please enter a number:'))
# Declaring and Initialization of two integer type variable
i = 2
flag = 0
while i<num:
if num%i == 0:
#If Yes,update flag value
flag = 1
print (num,"is NOT a prime number!");
#Updating the value of i on every iteration by 1
i = i + 1
#checking the value of flag
if flag == 0:
#If Yes, Then it is a prime number
print (num,"is a prime number!");``````

OUTPUT :

Explanation

In the program, the input method is used for fetching a number from the user to evaluate if it is a prime number. After converting the value to an integer, the value is stored in the variable num. Then, a variable i is assigned a value 2. The flag variable is assigned a value 0.

In the next line, A while loop is executed that runs as long as the i variable is less than the num variable. Inside the while loop, an if statement checks the modulation value of the number divided by the i variable. If the modulation value is 0, then the flag variable is assigned the value of 1. Then a break statement moves the control out of the loop.

Outside the loop, the i variable is incremented by 1. The last part of the code checks the flag value. If the value of the flag variable is equal to 0, a print statement displays "is a prime number" along with the number.

If the flag value is 1, the string "9 is not a prime number" is displayed.

So, when the user enters 9, the string is not prime a prime number” is printed. But, when the number 23 is entered, the string "23 is a prime number” is printed.

Conclusion

As a prime number is supposed to be a positive integer, the program checks the condition at the beginning. So, it is best to enter a positive value for checking whether it is prime or not.

Recommended Posts:

Contact Information

#3940 Sector 23,
Gurgaon, Haryana (India)
Pin :- 122015

contact@stechies.com

Top Tutorials

Top Interview Questions

All the site contents are Copyright © www.stechies.com and the content authors. All rights reserved. All product names are trademarks of their respective companies. The site www.stechies.com is in no way affiliated with SAP AG. Every effort is made to ensure the content integrity. Information used on this site is at your own risk. The content on this site may not be reproduced or redistributed without the express written permission of www.stechies.com or the content authors.

×