Python

Python Program to Print all Prime Numbers in an Interval2 min read

Write a program to Print all Prime Numbers in an Interval in Python

Code:




This Python program takes two inputs from the user, lower and upper, and finds all the prime numbers within the given range (lower to upper).

The program starts by asking the user to input the lower and upper range, which are stored as integers in the variables “lower” and “upper”, respectively.

Then, the program uses a for loop to iterate through all the numbers between the lower and upper range (inclusive). It starts by checking if the current number is greater than 1. If it is greater than 1, it uses another for loop to check if any number between 2 and the current number (excluding the current number) divides the current number completely. If any number between 2 and the current number divides the current number completely, it breaks out of the loop and goes to the next number. If no number between 2 and the current number divides the current number completely, it prints the current number to the output.

The program prints all the prime numbers between the lower and upper range, one number per line.

The output will be a list of integers, containing all the prime numbers between the lower and upper range, input by the user.

Leave a Comment