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.
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")
Enter a number: 9 9 is not a prime number
Enter a number: 23 23 is a prime number
In the above code, the input() method is used for obtaining ‘num’ value from the user. We know number less than or equal to 1 are not prime numbers thus we only perform an operation on the value if ‘num’ greater than 1.
If ‘num’ is greater than 1 is true the for loop is executed. This loop checks the numbers between 2 and the number entered by the user. For every number within this range, another if statement is executed with the code if (number % i) == 0. If this condition is True, a string is printed using the statement print(num, is not a prime number). Otherwise, a print statement print(num, is a prime number) is printed. The last else statement is executed when the number entered is less than or equal to 1.
According to the output, the user has entered 9 as the number. As it is not a prime number, the string "9 is not a prime number" is printed to the screen. But when 23 is entered, the string "23 is a prime number" is printed.
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!");
Enter a number: 9 9 IS NOT A PRIME NUMBER
Enter a number: 23 23 IS A PRIME NUMBER
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.
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.