I\'m a beginning programmer in python and have a question about my code I\'m writing:
number = int(input(\"Enter a random number: \"))
for num in range(1, n
With if (num % i) == 0:
you go to the else
block for every num
, which is not a multiply of 2, as you start i
with 2, thus printing num
. I got all odd numbers printed with your code.
You can use something like this:
number = int(input("Enter a random number: "))
for num in range(1, number + 1):
prime = True
for i in range(2, num):
if (num % i) == 0:
prime = False
break
if prime:
print(num)
It sets prime
to False
when it encounters a divisor without rest.