Related Articles

Related Articles

Python program to print all Prime numbers in an Interval
  • Difficulty Level : Medium
  • Last Updated : 17 Sep, 2020

Given two positive integers start and end. The task is to write a Python program to print all Prime numbers in an Interval.
Definition: A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are {2, 3, 5, 7, 11, ….}.
The idea to solve this problem is to iterate the val from start to end using a for loop and for every number, if it is greater than 1, check if it divides n. If we find any other number which divides, print that value.
Below is the Python implementation:
 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to print all
# prime number in an interval
#number should be greater than 1
start = 11
end = 25
 
for i in range(start,end):
  if i>1:
    for j in range(2,i):
        if(i % j==0):
            break
    else:
        print(i)

chevron_right


Output: 
 

11
13
17
19
23

The above solution can be optimized using the Sieve of Eratosthenes. Please see print prime numbers in a range for details.
 

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :