##### ► Python program to print all prime numbers in given range

Steps followed:
1. take range as input from user
2. validate user input by converting to integer
3. check if given range is valid
4. print all prime numbers falling in given range

Program
``````#python program to print all prime numbers in given range

#take range as input from user
lower_range,upper_range = input("Enter range in format lower range,upper range: ").split(",")

try:
lower_range=int(lower_range)
upper_range=int(upper_range)

# check if given range is valid
if upper_range <= lower_range:
print("invalid range")
else:
#2 is smallest prime number, hence there is no prime number below 2
if upper_range in (0,1,2):
print("there is no prime number below %d"%(upper_range))
else:
#print all prime numbers falling in given range
print("all prime numbers between %d and %d are:"%(lower_range,upper_range))
for j in range(lower_range,upper_range+1):
count=0
i=2
while i<j:
if j % i == 0:
count += 1
i += 1
else:
i += 1
#prime numbers are only divisble by 1 and the number itself
if count == 0:
print(j,end=" ")
else:
continue

except:
print("---error---")

Output
``````Enter range in format lower range,upper range: 5,80
all prime numbers between 5 and 80 are:
5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79

Enter range in format lower range,upper range: 0,2
there is no prime number below 2

Enter range in format lower range,upper range: 55,30
invalid range``````