This Python program demonstrates how you can check if a given number is a prime number or not.
A number is a prime number if the following confitions are satisfied
- It is a Positive Number
- It is Greater than 1.
- Has no other factors except 1 and the number itself.
Some of the examples of a prime number are 2,3 etc.
How to Check if a Given Number is a Prime Number or not in Python?
# Author : Abandantcode.com # Python Program to check if a number is prime number inputNumber = 5 if inputNumber > 1: for i in range(2,inputNumber): if (inputNumber % i) == 0: print(inputNumber,"is not a prime number") break else: print(inputNumber,"is a prime number") else: print(inputNumber,"is not a prime number")
5 is a prime number
In the above program , the input number is stored in the variable inputNumber. We first check if the number is greater than 1. If the number is less than or equals to 1 , it is a non-prime number.
We then check if the number is exactly divisible by any number and check the remainder to see if it is 0. If the remainder is 0, we can exit the loop as its a non-prime number.