Ask Question

5.16 LAB: Output numbers in reverse Write a program that reads a list of integers, and outputs those integers in reverse. The input begins with an integer indicating the number of integers that follow. For coding simplicity, follow each output integer by a space, including the last one. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: 10 8 6 4 2 To achieve the above, first read the integers into an array. Then output the array in reverse.

+3
Answers (1)
  1. 8 March, 00:24
    0
    The solution code is written in Python 3

    num_input = input ("Enter a list of number: ") num_list = num_input. split (" ") output = "" for i in range (len (num_list) - 1, 0, - 1) : output + = num_list[i] + " " print (output)

    Explanation:

    Firstly, use the input function to prompt user to enter a list of number in a single string (Line 1).

    Next, use the split method to convert the input string into a list of individual numbers using single space as delimiter (Line 2).

    Use a for loop and start the iteration from the last element in the list (Line 5) and then join the element to the output string followed with a single space (Line 5-6).

    After completing the loop, we print the output string (Line 8).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “5.16 LAB: Output numbers in reverse Write a program that reads a list of integers, and outputs those integers in reverse. The input begins ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers