Python Program to Print all Prime Numbers in an interval

Python program to print all prime numbers in an interval | A natural number that has only two factors (1 and itself ) is called a prime number. For example- 5 is a prime number because it has only two factors 1 and 5. Similarly, 9 is not a prime number because it has more than 2 factors that are 1,3, and 9.

To develop a Python program to print all prime numbers in an interval; first, you should know how to develop a Python program to find out all factors of a number. Because if any number has more than 2 factors then only, it is a prime number. All negative numbers, 0 and 1 are not the prime numbers.

Python program to print all prime numbers from 1 to 100

This python program using the for loop and if-else statement. We will take a range from 1 to 100 while declaring the variables. Then, print all prime numbers in an interval using the for loop and finally, the result will be displayed on the screen.

# Python program to print all prime numbers from 1 to 100

# take range
start = 1
end = 100

print('All prime numbers from 1 to 100')

for n in range(start, end+1):
    # all prime numbers are greater than 1
    if n > 1:
        for i in range(2, n):
           if (n % i) == 0:
               break
        else:
           print(n, end=' ')

Output:-

All prime numbers from 1 to 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

First n Prime Numbers in Python

We will develop a Python program to print all prime numbers from 1 to N. First, take the upper range(N) as input. Then use a for loop to iterate the numbers from 1 to N and print all prime numbers.

# Python program to print first n prime numbers

# take range
N = int(input('Enter upper range: '))

print('All prime numbers from 1 to ', N)

for n in range(1, N+1):
    # all prime numbers are greater than 1
    if n > 1:
        for i in range(2, n):
           if (n % i) == 0:
               break
        else:
           print(n, end=' ')

Output:-

Enter upper range: 55
All prime numbers from 1 to 55
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53

Python Find Prime Numbers in Range

In the previous program, we will print all prime numbers from 1 to N but in this program, print all prime numbers in any interval (range).

# Python program to print all prime numbers in an interval

# take range
start = int(input('Enter lower range: '))
end = int(input('Enter upper range: '))

print('All prime numbers in the range')

for n in range(start, end+1):
    # all prime numbers are greater than 1
    if n > 1:
        for i in range(2, n):
           if (n % i) == 0:
               break
        else:
           print(n, end=' ')

Output for the different input values:-

Enter lower range: 25
Enter upper range: 50
All prime numbers in the range
29 31 37 41 43 47

Enter lower range: 200
Enter upper range: 400
All prime numbers in the range
211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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