Algorithm
-
Define a function
is_prime
that takes a number as input:- If the number is less than 2, return False (as numbers less than 2 are not prime).
- Iterate from 2 to the square root of the number.
- If the number is divisible evenly by any of the iterated values, return False (indicating it is not prime).
- If no divisors are found, return True.
- Define a function
print_primes_in_interval
that takes start and end values as input:- Iterate through each number in the specified range.
- For each number, check if it is prime using the
is_prime
function. - If it is prime, print the number.
- Get user input for the start and end values of the interval.
-
Call the
print_primes_in_interval
function with the user-provided start and end values.
Code Examples
#1 Example- For finding the python prime number between a specified interval
Code -
Python Programming
lwr = 900
upr = 1000
print("Prime numbers between", lwr, "and", upr, "are:")
for numb in range(lwr, upr + 1):
# all prime numbers are greater than 1
if numb > 1:
for a in range(2, numb):
if (numb % a) == 0:
break
else:
print(numb)
Copy The Code &
Try With Live Editor
Output
Prime numbers between 900 and 1000 are:
907
911
919
929
937
941
947
953
967
971
977
983
991
997
907
911
919
929
937
941
947
953
967
971
977
983
991
997
#2 Example- Find the Prime Numbers from 1 to 100 in Python
Code -
Python Programming
for Numb in range (1, 101):
cnt = 0
for a in range(2, (Numb//2 + 1)):
if(Numb % a == 0):
cnt = cnt + 1
break
if (cnt == 0 and Numb != 1):
print(" %d" %Numb, end = ' ')
Copy The Code &
Try With Live Editor
Output
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
Demonstration
Python Programing Example to Print all Prime Numbers in an Interval-DevsEnv