First N odd, ascending. Given an integer N, display all the odd numbers from 1 to N in ascending order.
|
n = int(input()) for i in range(1, n + 1, 2): print(i) |
or
|
N = int(input()) for number in range(1, N + 1): if number % 2 != 0: print(number) |
Explanation:
- Reading Input:
- The
input()
function reads a line of text from the user.
- The
int()
function converts the input text to an integer and stores it in the variable N
.
- For Loop:
for number in range(1, N + 1):
- A
for
loop iterates over the range of numbers from 1 to N
(inclusive).
- Odd Number Check:
- Inside the loop, each number is checked to see if it is odd using the modulus operator
%
.
- If the number is odd (i.e.,
number % 2
is not equal to 0), it is printed.
Example Execution:
If the user inputs 10
, the output will be:
1
3
5
7
9
This code will read the number N
from the user and print all odd numbers from 1 to N
in ascending order.