codescracker


python

Python Program to Print Prime Numbers



« Previous Program Next Program »


Print Prime Numbers in Python

To print prime numbers in python, you have to ask from user to enter the limit in which he/she want to print all the prime numbers. As you know that prime numbers are those numbers which are divisible by only 1 and the number itself.

Therefore, to print all the prime numbers between the given range, you have to check all the number in the given range, whether the number is prime or not, if the number is find to be prime number, then print that number and check for the next number until the range reached as shown in the program given below.

Python Programming Code to Print Prime Numbers

Following python program ask from user to enter the range to print all the prime numbers present in the given range:

# Python Program - Print Prime Numbers
		
while True:
	print("Enter 'x' for exit.")
	print("Enter starting and ending number: ")
	start = input()
	end = input()
	if start == 'x':
		break
	else:
		lower_number = int(start)
		upper_number = int(end)
		print("\nPrime Numbers between the given range:")
		for num in range(lower_number, upper_number+1):
			if num>1:
				for i in range(2, num):
					if(num%i)==0:
						break
				else:
					print(num)

Here is the sample run of the above python program to illustrates how to print all the prime numbers between the range provided by the user:

python print prime numbers

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