Skip to content
Sign upLog in
← Back to Community
How do i make a program that prints out only odd numbers from range? PYTHON
Profile icon
megapega88

from range that the user sets for example: user says "10" output: 1 3 5 7 9

Answered by SixBeeps [earned 5 cycles]
View Answer
Voters
Profile icon
megapega88
Comments
hotnewtop
Profile icon
SixBeeps

Use the normal for-loop, but before printing the number, check to see if it is divisible by two or not (the remainder of the number divided by two is 0 if it is divisible). If it isn't, print the number. Otherwise, do nothing.

Profile icon
megapega88

@SixBeeps
Thank you!

Profile icon
SamDev7

@megapega88
There is an easier way

for i in range(1,10,2): print(i)

Output

1 3 5 7 9
Profile icon
megapega88

@Saml2020
yeah, i just wanted help on how do i make it print out from not a set number. likei can say 22 or 247 and it wouldn't matter. but thanks for replying

Profile icon
SamDev7

@megapega88
Simply change the 10 in the code above 😉