Python program to check given number is prime number or not.


Program to check given number is prime number or not.


In this post we will see a program to check given number is prime number or not. Let's start..!

To write this program first we need to know How we can identify the number is prime or not ? 

You have already studied in mathematics what is prime number. So you know all prime number have only two factor that 1 and the number itself. It means the number should have only two factors that 1 and the number itself then that number will be prime number otherwise that number will be not a prime number. 

Now we know that how we can identify a given number is prime or not.

Let's create a algorithm. 

[ if don't know about algorithm then click here to learn more about Algorithm ]


Algorithm :

step 01: Start

step 02: Create two variables 'number' and 'count'

step 03: Print a message "Enter a number :: "

step 04: Initialize a count to 2 ( count = 2)

step 05: Take a input from user and stored it in variable number

step 06: Check a condition ( number % count != 0 ). If condition is false then goto step 09 Or if condition is true then follow the following steps

step 07: Increment a counter by one i.e. count = count + 1

step 08: goto step 06 and follow all the following steps 

step 09: Again check a condition ( count == number ) if condition is true then print a message "prime number !" Or if condition is true then print a message "Not a prime number !" 

step 10: Stop


Explanation : 

The step 01 simply says start your algorithm or solution.

The step 02 simply says that create a variable named as number ( here your variable name can be anything not mandatory to have you variable name same as I shown here ) and count. The variable number will store a value entered by user; and variable count will use as counter. 

[ if you don't know how to create variables; click here to learn more about variables in Python]

The step 03 says that simply print a  message "Enter a number :: " to tell a user to enter a number that user want to check prime number or not a prime number.

The step 04 says that initialize your counter variable to 2; because we know that smallest prime number is 2.

The step 05 says simply take a input from user and store that number in variable number. 

[ if you don't know how to take input from user; click here to learn more about input( ) in python ]

The step 06 says that check a condition ( number % count ) using while loop because we want to repeat the steps again and again.   

The step 07 says simply increment the counter by one (1) in while block.

[ if you don't know how to use while loop; click here to learn more about While loop in Python ]

The step 08 says simply repeat the for loop until condition becomes false.

The step 09 says again check a condition ( count == number ) using if else. If condition is true then print a message "Prime number" ( here you can print any text that you want. ). Or if condition is false then print a message "Not a prime number" ( here you can print any text that you want. ). 

[ if you don't know how to use if else; click here to learn more about if else in Python ]

The step 10 says that your problem has solved so stop.

Now write a Program according to this algorithm. 


Program : 

# start
# Program to check given number is prime or not 

# creating variables
count = 0
number = 0

print("Enter a number :: ")
# initializing a counter
count = 2
# taking a input from user
number = int(input())

# cheking condition in while loop
while number % count != 0 :
    # condition is true
    # increment a counter by 1
    count = count + 1

if count == number : 
    # condition is true 
    # number is prime number
    print("Prime number !")
else :
    # condition is false 
    # number is not a prime number
    print("Not a prime number !")

# stop


Output : 

sample input and output 1


Enter a number ::
10
Not a prime number !






sample input and output 2


Enter a number ::
7
Prime number !







If you have any doubts in this post then please ask me in comment section below. I will try to solve it.


If you like these post and you have learned something from this then share this post with your friends and family.

Keep learning always.....!!! 😇😇😇








Previous Post  Next Post