Print Prime Numbers in Range
Print all prime numbers in a given inclusive range.
BeginnerTopic: Loop Programs
Python Print Prime Numbers in Range Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# Program to print prime numbers in a range
start = int(input("Enter start of range: "))
end = int(input("Enter end of range: "))
for num in range(start, end + 1):
if num < 2:
continue
is_prime = True
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num)Output
Enter start of range: 1 Enter end of range: 10 2 3 5 7
Understanding Print Prime Numbers in Range
We test each number in the range for primality by checking divisibility up to its square root.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.