Program to Print all Prime Numbers in an Interval in Python. This program accept a range (Upper and lower limit) and display all the prime number exists in that range. To perform operation this program uses if else conditional statement and for loop knowledge. This is a important program for beginners, class 11, 12 and B Tech computer science student. Download source code.
Program to Print all Prime Numbers in an Interval in Python
What is a Prime Number ?
A prime number is a natural number which is greater than 1 and has no positive divisor other than 1 and itself, such as 2, 3, 5, 7, 11, 13, and so on.
Program task :
The user is given two integer numbers, lower value, and upper value. The task is to write the Python program for printing all the prime numbers between the given interval (or range).
Source code Program to Print all Prime Numbers in an Interval in Python :
# First, we will take the input:
lower_value = int(input ("Please, Enter the Lowest Range Value: "))
upper_value = int(input ("Please, Enter the Upper Range Value: "))
print ("The Prime Numbers in the range are: ")
for number in range (lower_value, upper_value + 1):
if number > 1:
for i in range (2, number):
if (number % i) == 0:
break
else:
print (number)
Download Source code : For Download Click on the below Button
Step By step Programing logic :
To print all the prime numbers between the given interval, the user has to follow the following steps:
Step 1: Loop through all the elements in the given range.
Step 2: Check for each number if it has any factor between 1 and itself.
Step 3: If yes, then the number is not prime, and it will move to the next number.
Step 4: If no, it is the prime number, and the program will print it and check for the next number.
Step 5: The loop will break when it is reached to the upper value.
Output :
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> RESTART: C:/Users/ARCHIT/AppData/Local/Programs/Python/Python37-32/test1.py Please, Enter the Lowest Range Value: 2 Please, Enter the Upper Range Value: 50 The Prime Numbers in the range are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 >>>
Output Explanation :
The lowest range value is entered
Please, Enter the Lowest Range Value: 2
The highest range value
Please, Enter the Upper Range Value: 50
Prime number is mathematical calculated in the range and printed
The Prime Numbers in the range are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Conclusion :
The program inputs the range and prints all the prime number in between the range. The programs runs fine.
Thank you for reading and learning from Program to Print all Prime Numbers in an Interval in Python Post. This program uses mathematical knowledge of prime numbers, if else conditional statement and for loop knowledge. This is a important program for beginners, class 11, 12 and B Tech computer science student.
Python Programs Archives – CS Study
Computer Science Practical file for class 12 Term-2 – CS Study
Computer Science with Python Class 11 Sumita Arora Solutions – CS Study