Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

item 2 10 points while loop counting by codechum admin write a program …

Question

item 2
10 points
while loop counting
by codechum admin
write a program that takes an integer n.use a
while loop to count from 1 to n (inclusive), and
print each number on a new line.
sample output 1
enter a number: 10
1
2
3
4
5
6
7
8
9
10
sample output 2
enter a number: 7
1
2
3
4
5
6
7
sample output 3

Explanation:

Step1: Get user input

n = int(input("Enter a number: "))

Step2: Initialize counter

i = 1

Step3: Use while loop

while i <= n:
    print(i)
    i += 1

Answer:

n = int(input("Enter a number: "))
i = 1
while i <= n:
    print(i)
    i += 1