Python program to check given number is prime number or not.
Program to check given number is prime number 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 :
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 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 :
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.....!!! 😇😇😇