Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Looping Statements

Print all prime numbers in a given range - Python Program

Example 1 :

# Prime numbers in a range start = int(input("Enter start: ")) end = int(input("Enter end: ")) print(f"Prime numbers between {start} and {end}:") for num in range(start, end + 1): if num > 1: 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, end=" ")

Output

 
OUTPUT  :
Enter start: 5
Enter end: 25
Prime numbers between 5 and 25:
5 7 11 13 17 19 23 
 

Example 2 : Advanced Program

def find_primes_in_range(lower_limit, upper_limit): """ Prints all prime numbers within a given range (inclusive). Args: lower_limit (int): The lower bound of the range. upper_limit (int): The upper bound of the range. """ if lower_limit > upper_limit: print("Error: The lower limit cannot be greater than the upper limit.") return print(f"Prime numbers between {lower_limit} and {upper_limit} are:") for num in range(lower_limit, upper_limit + 1): # All prime numbers are greater than 1 if num > 1: is_prime = True # Check for factors from 2 up to the square root of num # We only need to check up to the square root because if a number # has a factor greater than its square root, it must also have a # factor smaller than its square root. for i in range(2, int(num**0.5) + 1): if (num % i) == 0: is_prime = False break # Not prime, no need to check further if is_prime: print(num) # Example Usage: lower = 10 upper = 50 find_primes_in_range(lower, upper) print("\nAnother example:") lower = 1 upper = 10 find_primes_in_range(lower, upper)

Output

 
OUTPUT  :
Prime numbers between 10 and 50 are:
11
13
17
19
23
29
31
37
41
43
47

Another example:
Prime numbers between 1 and 10 are:
2
3
5
7
 

Explanation:

find_primes_in_range(lower_limit, upper_limit) function:

This function takes two integer arguments: lower_limit and upper_limit, representing the inclusive range within which to find prime numbers.

Input Validation:

It first checks if lower_limit is greater than upper_limit. If so, it prints an error message and exits, as an invalid range is provided.

Iteration through Range:

The program then iterates through each number (num) from lower_limit to upper_limit (inclusive) using a for loop and the range() function.

 

Prime Number Check:

  • It checks if numis greater than 1, as 1 is not considered a prime number.
  • A boolean variable is_primeis initialized to True for each num, assuming it's prime until proven otherwise.
  • An inner forloop iterates from 2 up to the integer part of the square root of num. This optimization is based on the property that if a number num has a divisor, it must have one less than or equal to its square root.
  • Inside the inner loop, it checks if numis divisible by i (i.e., num % i == 0). If it is, num is not prime, so is_prime is set to False, and the inner loop is break
  • If the inner loop completes without finding any divisors, is_primeremains True, and num is printed as a prime number.