In this tutorial, we will be learning to write Python program to Print prime numbers in a given range.
A prime number is a number that is divisible only by 1 and itself. In this post we will discuss multiple approaches to write a Python program to print all prime numbers in a given range.
Method 1: Brute Force Method
# Brute Force Method
import math
start = int(input("Please enter starting number: "))
end = int(input("Please enter end number: "))
for num in range(start, end+1):
if num > 1:
for i in range(2, int(math.sqrt(num))+1):
if num % i == 0:
break
else:
print(num, end=" ")
Output:
Enter starting number: 4
Enter ending number: 45
5 7 11 13 17 19 23 29 31 37 41 43