In this tutorial, we will learn to write a Python program to print all prime numbers between an interval. For example, if you want to print all prime numbers between 1 to 100, you can use the below program.
Prime number is a number that has only two factors, 1 and itself but 1 is not a prime number. Prime numbers are: 2, 3, 5, 7, 11 etc.
Program 1: Python program to print all prime numbers in an interval
#take user input from starting to ending number
num1 = int(input("Please enter starting number: "))
num2 = int(input("Please enter ending number: "))
# loop through start to end
for i in range(num1, num2 + 1):
if i > 1:
# loop through 2 to square root of i + 1
for j in range(2, int(i**0.5) + 1):
if i % j == 0:
break
else:
print(i)
output
Please enter starting number: 1
Please enter ending number: 100
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
Explanation:
- First, we are taking two numbers as input from the user.
- Start the loop from num1 to num2 + 1. It will iterate each number between num1 to num2 including num1 and num2.
- Inside first loop, if variable “i” is less than or equal to 1 then skip the step and go to next iteration.
- If number is greater than 1 then start a loop from 2 to (square root of i + 1).
- Next, if i is divisible by j then it will not be a prime number and if i is not divisible by any number from the range of inner loop then it will be a prime number.
- In the else condition of for each loop, we are printing the value of i to print all prime numbers between given numbers.
Program 2: Python program to print all prime numbers in an interval using functions
# program to print all prime numbers between two numbers by using function
def is_prime_number(num: int):
"""
Function helps to check that given number is a prime or not
Return True if number is prime else False
@param num: int
@return bool
"""
# check whether input number is less than 1 or not
if num <= 1:
return False
# Check for factors from 2 to the square root of the number + 1
for i in range(2, int(num**0.5) + 1):
# if number is divisble by any number from the range then it is not prime number
if num % i == 0:
return False
return True
def display_all_prime_number(num1: int, num2: int):
"""
Display all prime numbers between two given numbers including num1 and num2
For example, if num1 = 1 and num2 = 5 then prime numbers are 2, 3, 5
@param num1: int
@param num2: int
@return None
"""
for i in range(num1, num2 + 1):
if i > 1:
if is_prime_number(i):
print(i)
#take user input from starting to ending number
num1 = int(input("Please enter starting number: "))
num2 = int(input("Please enter ending number: "))
# call function to print all prime numbers
display_all_prime_number(num1, num2)
Output
Please enter starting number: 3
Please enter ending number: 11
3
5
7
11
In the second program, we are using two functions. One for checking number is prime or not and another one for displaying all prime numbers between given two numbers. In the output screen, I have given starting number as 3 and ending number as 11 then there are total 4 prime numbers between and including both; 3, 5, 7, 11
You may like other programs: