Python Program to Print all Prime Numbers in an Interval


In this program, you'll learn to print all prime numbers within an interval using for loop and diplay it.

A positive integer greater than 1 which has no other factors except 1 and the number itself is called a prime number.

2,3,5,7 etc. are prime number as they do not have any other factors. But 6 is not prime (it is composite) Since, 2 * 3 = 6.


# Python program to display all the prime numbers within an interval

# change the values of lower and upper for a different result
lower = 900
upper = 1000

# uncomment the following lines to take input from the user 
# lower = int(input("Enter lower range:")) 
# upper = int(input("Enter upper range:")) 

print("Prime number between",lower,"and",upper,"are:")

for num in range(lower,upper + 1):
    # prime number are greater than 1
   if num> 1:
        for i in range (2,num):
              if(num % i) == 0:
              break
       else:
              print(num)

Output

Prime numbers between 900 and 1000 are :
907
911
919
929
937
941
947
953
967
971
977
983
991
997

Here, we store the interval as lower for lower interval and upper for upper interval, and find prime numbers in that range. Visit this page to understand the code to check for prime numbers.