Write a program to display the numbers in reverse order 20-1. (using for loop).
Answers
Answer:
10 9 8 7 6 5 4 3 2 1
Explanation:
Input: N = 20
Output: 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Run a loop from 20 to 1 and print the value 20 for each iteration. Decrement the value of 20 by 1 after each iteration.
Below is the implementation of the above approach.
for example ,
Given a number N, the task is to print the numbers from N to 1.
Input: N = 10
Output: 10 9 8 7 6 5 4 3 2 1
Run a loop from N to 1 and print the value of N for each iteration. Decrement the value of N by 1 after each iteration.
Below is the implementation of the above approach.
5 4 3 2 1
Answer:
Software Used:
- Python
Required program:
for i in range (20,0,-1):
print(i)
Explanation:
In this program we have set initial value to 20, final values to 0 and step value to -1. We have set initial value to 20 and final value to 0 as we want our program to print the numbers 20-1 in reverse order. We have set step value to -1 as we want our numbers in the range decrease from 20 to 1. We haven't set final value to 1 as final value takes the value of 1 number less if the range is increasing, and it takes value of 1 number more if the range is decreasing. (i.e. If we want 20 as a final value we will write the final value in the program as 21.)
Output:
The output of the following program will be as follows:
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1