Home » Python Print all Prime Numbers in an Interval

Python Print all Prime Numbers in an Interval

by Online Tutorials Library

Python Program to Print all Prime Numbers in an Interval

A prime number is a natural number which is greater than 1 and has no positive divisor other than 1 and itself, such as 2, 3, 5, 7, 11, 13, and so on.

The user is given two integer numbers, lower value, and upper value. The task is to write the Python program for printing all the prime numbers between the given interval (or range).

To print all the prime numbers between the given interval, the user has to follow the following steps:

  • Step 1: Loop through all the elements in the given range.
  • Step 2: Check for each number if it has any factor between 1 and itself.
  • Step 3: If yes, then the number is not prime, and it will move to the next number.
  • Step 4: If no, it is the prime number, and the program will print it and check for the next number.
  • Step 5: The loop will break when it is reached to the upper value.

Example: The Python Code to Print the Prime Number between the given Interval.

Output:

Please, Enter the Lowest Range Value:  14  Please, Enter the Upper Range Value:  97  The Prime Numbers in the range are:   17  19  23  29  31  37  41  43  47  53  59  61  67  71  73  79  83  89  97  

Conclusion

In this tutorial, we have shown how to write the code to print the prime numbers between the given interval of numbers.


You may also like