Ask coding questions

← Back to all posts
How do i make a program that prints out only odd numbers from range? PYTHON
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
megapega88
Comments
hotnewtop
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.

megapega88

@SixBeeps Thank you!

Saml2020

@megapega88 There is an easier way

Output

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

Saml2020

@megapega88 Simply change the 10 in the code above 😉