Program to Find Prime Numbers in a Given Range in Python
Finding Prime Numbers in a Range
A prime number is a natural number greater than 1 that has no divisors other than 1 and itself.
We will explore different methods to find all prime numbers within a given range using Python programming.
Method 1: Using a for Loop
We iterate through the given range and check if each number is prime.
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
start = int(input("Enter the start of the range: "))
end = int(input("Enter the end of the range: "))
print(f"Prime numbers between {start} and {end} are:", end=" ")
for num in range(start, end + 1):
if is_prime(num):
print(num, end=" ")
Output:
Enter the start of the range: 10 Enter the end of the range: 20 Prime numbers between 10 and 20 are: 11 13 17 19