In this Python program, we will learn how to check if the given number is prime or not.
A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1.
For example, 7 is prime because the only ways of writing it as a product, 1 × 7 or 7 × 1, involve 7 itself.
Here is the source code to check if the given number is prime or not
Python Program to Check if the Number is Prime Number or Not
# Python Program to Check if the Number is Prime Number or Not
# To take input from the user
num = int(input("Enter a number: "))
# prime numbers are greater than 1
if num > 1:
# check for factors
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")
# if input number is less than or equal to 1, it is not prime
else:
print(num,"is not a prime number")
Enter a number: 5
5 is a prime number
Enter a number: 6
6 is not a prime number
Enter a number: 87
87 is not a prime number
Comments