In this tutorial, we will learn to write a Python program to check whether a number is a prime number or not. First, we will understand what is prime number before going to writing a program.
What is the Prime number?
A Prime number is a natural number greater than 1 (one), that has only two factors, 1 and itself. In other words, a positive integer, except for 1 (one), that is divisible only by 1 and itself is called a prime number. Two (2) is the only even prime number. Other even numbers are divisible by 2 or other even numbers, which is why they are not considered prime numbers.
Examples of prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199 etc.
Program 1: Check if a number is a prime number or not
# program to check input number is prime number or not
# take a input number from the user
num = int(input("Please enter a number: "))
# check whether input number is less than 1 or not
if num <= 1:
print(f"Input number {num} is not prime number.")
else:
# Check for factors from 2 to the square root of the number + 1
for i in range(2, int(num**0.5) + 1):
# if number is divisble by any number from the range then it is not prime number
if num % i == 0:
print(f"Input number {num} is not prime number.")
break
else:
print(f"Input number {num} is prime number.")
Output
Please enter a number: 9
Input number 9 is not prime number.
Explanation:
- In the above program, we are taking input from the user and storing that input number in the num variable in integer form.
- After that, we are checking that whether the input number is less than 1 or not because numbers less than 1 are not prime numbers. If the condition is True then print the statement.
- If the first condition does not match, then enter into the else part and execute a for loop from 2 to (square root of num + 1). For example, if a user is entering the number 19 then the square root of 19 is 4.358898943540674. When you convert this number in integer form then you will get integer part only. So, here the square root of 19 will be 4, and adding a number 1 to the square root, then it will become 5 according to (square root of num + 1). The main fact is that a composite number must have a factor less than or equal to the square root of that number. Otherwise, the number is not a prime number.
- The for loop range will begin from 2 to 5 which means 2, 3, and 4 will be iterated. In Python, when we give a range(2, n) means the range starts from 2 but ends at (n-1).
- Inside the loop, it will check that the number is divisible by iterated numbers, if yes then it will not be a prime number and break the loop.
- If the loop is completely iterated and does not fulfill the if condition inside the loop then the else part will be executed and print the statement that the number is prime.
Program 2: Check if a number is a prime number or not by using a function
I am going to teach you how to write the above program by using a function. You should learn to write a program in different ways because, in most examinations or interviews, questions will be asked to write a program in different ways.
# program to check input number is prime number or not by using function
def is_prime_number(num):
# check whether input number is less than 1 or not
if num <= 1:
return False
# Check for factors from 2 to the square root of the number + 1
for i in range(2, int(num**0.5) + 1):
# if number is divisble by any number from the range then it is not prime number
if num % i == 0:
return False
return True
# take a input number from the user
num = int(input("Please enter a number: "))
# Check if the input number is prime
if is_prime_number(num):
print(f"Input number {num} is prime number.")
else:
print(f"Input number {num} is not prime number.")
Output
Please enter a number: 13
Input number 13 is prime number.
In the above program, I used the same logic to check whether the input number is prime or not in a different way than the first program. I converted the first program into a function so that it will be easy to create a function and use it somewhere. In this tutorial, we learned to write a Python program to check prime numbers.