Write a program in python to print all prime numbers in an Interval.

A number is said to be Prime if it doesn’t have any factors other than 1 and itself. Example: 3, 5, 7, etc.

To print all prime numbers in python we have to:

  1. Loop through all elements in the given interval.
  2. Check for each number if it has any factor between 1 and itself.
  3. If yes then it is not prime, move to next number.
  4. Else it is a Prime.

Let’s see the implementation of the same in python.

lower = int(input("Enter the lower Interval value: "))
upper = int(input("Enter the upper Interval value: "))

#loop through the Interval and check individually
for num in range(lower, upper+1):

    #Number should not be divisible
    #by any number between 1-num
    for x in range(2,num):
        if num%x==0:
            break
    else:
        print(num)

Output

all prime numbers between 10 to 20

Check out more python programming examples.

Leave a Reply