Python program to check whether a given number is prime or not
Python program to check whether a given number is prime or not
A positive integer greater than 1 is called a prime number if it is divisible by one and number itself except 1.
Example: 2,3, 7, 11, 13, 17, 19 are prime numbers, while 6 is not a prime number.
Source Code
The following is a source code to check whether the number is prime or not:
#Program to check a given number is prime or not #Take input from the user number = int(input("Enter the number to be checked:")) # Check number is greater than 1 if number > 1: # Check for factors for i in range(2, number): if (number % i) == 0: print(number, "is not a prime number") break else: print(number, "is a prime number") else: print(number, "is not a prime number")
Output
Here is the result:
Case 1:

Case 2:

Explanation: In this program, we will check the number stored in a variable number is prime or not. The prime number is always greater than 1. So we will proceed only if the number is greater than 1.
The loops will be iterated from 2 to number -1. We will check if the number is completely divisible by any number from 2 to number -1. If there exists any factor, then it is not prime. Else the number is prime.