Python Program to Print all Prime Numbers in an Interval – 2024

Hey guys, in this blog we will see a Python Program to Print all Prime Numbers in an Interval.

Code

# Python Program to Print all Prime Numbers in an Interval

lower = int(input('Enter the lower bound of range -> '))
upper = int(input('Enter the upper bound of range -> '))

print(f"Prime numbers between {lower} and {upper} are: ")

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

Output

Enter the lower bound of range -> 2
Enter the upper bound of range -> 30
Prime numbers between 2 and 30 are: 
2
3
5
7
11
13
17
19
23
29
  • Here we are simply running a loop from the lower bound to the upper bound and checking for every number that if it’s a prime number or not.
  • We are using the same logic for finding the prime numbers that we used here – Python Program to Check Prime Numbers

Check out our other python programming examples

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 514

Leave a Reply

Your email address will not be published. Required fields are marked *