# Python - Find the number of prime numbers within a given range of numbers

PythonServer Side ProgrammingProgramming

When it is required to find the prime numbers within a given range of numbers, the range is entered and it is iterated over. The ‘%’ modulus operator is used to find the prime numbers.

## Example

Below is a demonstration of the same

lower_range = 670
upper_range = 699
print("The lower and upper range are :")
print(lower_range, upper_range)
print("The prime numbers between", lower_range, "and", upper_range, "are:")
for num in range(lower_range, upper_range + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)

## Output

The lower and upper range are :
670 699
The prime numbers between 670 and 699 are:
673
677
683
691

## Explanation

• The upper range and lower range values are entered and displayed on the console.

• The numbers are iterated over.

• It is checked to see if they are greater than 1 since 1 is neither a prime number nor a composite number.

• The numbers are iterated, and ‘%’ with 2.

• This way the prime number is found, and displayed on console.

• Else it breaks out of the loop.

Published on 20-Sep-2021 11:01:52