Python Program to Print Numbers in an Interval


In this article, we will learn about the solution and approach to solve the given problem statement.

Problem statement

Given the starting and ending range of an interval. We need to print all the numbers in the interval given.

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.

There are two for loops, first for loop is for getting the numbers in the interval and second loop for the checking whether the number is prime or not.

Now let’s see the implementation.

Example

 Live Demo

start = 10
end = 29
for val in range(start, end + 1):
# If num is divisible by any number is not prime
   if val > 1:
      for n in range(2, val):
         if (val % n) == 0:
            break
      else:
         print(val)

Output

11
13
17
19
23
29

All variables and functions are declared in the global scope as shown in the figure below.

Conclusion

In this article, we learned about the approach to print numbers in a given interval.

Updated on: 27-Sep-2019

358 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements