codescracker


python

Python Program to Check Prime Numbers



« Previous Program Next Program »


Check Prime Numbers in Python

To check whether the input number is a prime number or not in python, you have to ask from user to enter a number and start checking the number for prime number as shown in the program given here.

Python Programming Code to Check Prime Numbers

Following python program ask from user to enter any number to check whether it is a prime number or not:

# Python Program - Check Prime Number or Not
		
while True:
	print("Enter 'x' for exit.")
	num = input("Enter any number: ")
	if num == 'x':
		break
	try:
		number = int(num)
	except ValueError:
		print("Please, enter a number...")
	else:
		for i in range(2, number):
			if number%i == 0:
				print(number, "is not a prime number.\n")
				break
		else:
			print(number, "is a prime number.\n")

Here is the sample run of the above Python program illustrating how to check whether a number is a prime number or not a prime number:

python check prime number

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test