Computer Science, asked by ashisharya9651, 3 months ago

Write a program to print reverse no(10,9,8,7,6,5,4,3,2,1) using Loop Statements.​

Answers

Answered by Tomboyish44
14

The for-loop has been used in this program. The co‎‎‎de is as follows:  

[Written using Python 3]

__________________

print("This program will allow you to print numbers in the reverse order starting from a given number to 1.\n")

number = int(inpu‎‎‎t("Enter the number till which you'd like to display numbers (In reverse order): "))

print()

for count in reversed(range(2, number + 1)):

   print(count, end = ", ")

print("1.")

__________________

Explanation:

First line: print("This program will allow you to print numbers in the reverse order starting from a given number to 1.\n")

  • Printing/Stating the purpose of the program.

Second line: number = int(inp‎‎‎ut("Enter the number till which you'd like to display numbers (In reverse order): "))

  • Taking the start value as in‎‎‎put from the user to start the count.

Third line: print()

  • Printing an empty line. [Optional]

Fourth line: for count in reversed(range(2, number + 1)):

  • Initiating the for loop in-order to traverse through the given range, the start value for the range is 2, and the stop value is "number". We're reversing the range using reversed(), which makes the stop value the start value and vice-versa. [1 is added to number in order to include the number given by the user in the output]

Fifth line:    print(count, end = ", ")

  • [This line of co‎‎‎de is in the body of the for-loop]. We're printing "count" for every number "count" traverses through the range, and setting the end parameter as ", ".

Sixth line: print("1.")

  • The start value in the range was set to 2, so it won't print 1 in the output. Just for the sake of ending the list with a full-stop instead of a comma that's set as the end parameter, I've printed it outside the body of the loop. [Once again, it's optional, you can include 1 in the range if you'd like to]

Output is attached in the screenshot.

Attachments:
Similar questions