Python program to check if a number is prime or not
In this Python programming guide, we are going to learn
-
program to check if a number is prime or not in python
Program to check if a number is prime or not in Python
The python program to check if a number is prime or not is as follows:
# Owner : TutorialsInhand Author : Devjeet Roy
number = int(input("Enter number: "))
if number > 1:
for i in range(2,(number//2)+1):
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")
The output of python program to check if a number is prime or not is as follows:
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter number: 81
81 is not a prime number
Few important tips about this program
1. We take a user input and convert it to int data type.
2. Then to check if a number "n" is prime or not we repeat the loop upto n/2 (inclusive). If any number within that range divides it then the number is not prime, else the number is prime.
3. Finally, we print the answer on the terminal.
Python program to find out whether a number is prime or not snapshot:
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :
Published Date :
Dec 30,2021