Why is output repeating?
lower = int(input("enter lower interval: "))
upper = int(input("enter upper interval: "))
for num in range(lower,upper):
if num>1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
Output:
enter lower interval: 2
enter upper interval: 15
3
5
5
5
7
7
7
7
7
9
11
11
11
11
11
11
11
11
11
13
13
13
13
13
13
13
13
13
13
13
CodePudding user response:
You should write the else statement for the for loop, not the if statement
upper = int(input("enter upper interval: "))
for num in range(lower,upper):
if num>1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
CodePudding user response:
It repeats because of having there are multiple numbers that can not divide that number in the given range. Try this hope will work:
lower = int(input("enter lower interval: "))
upper = int(input("enter upper interval: "))
for num in range(lower,upper):
is_prime = True
if num > 1:
for i in range(2, num):
if (num % i) == 0:
is_prime = False
if is_prime == True:
print(num)
I have checked if it pass the condition of if (num % i) == 0:
then the number is not Prime