Python Program to Check Prime Number


This content was proof-read by Grammarly. You can Try Grammary for FREE

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


Are you a .NET Developer planning to integrate GraphQL for .NET in your Application? Check out this book
  • 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")

Output

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.

%d bloggers like this: