Write a program to Print all Prime Numbers in an Interval in Python
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# Python program to display all the prime numbers within an interval # change the values of lower and upper for a different result lower = int(input("Enter lower range: ")) upper = int(input("Enter upper range: ")) print("Prime numbers between",lower,"and",upper,"are:") for num in range(lower,upper + 1): # prime numbers are greater than 1 if num > 1: for i in range(2,num): if (num % i) == 0: break else: print(num) |
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.