46.7k views
3 votes
Write a program that reads a list of 10 integers, and outputs those integers in reverse. For coding simplicity, follow each output integer by a space, including the last one. Then, output a newline.Ex: If the input is 2 4 6 8 10 12 14 16 18 20, the output is:20 18 16 14 12 10 8 6 4 2To achieve the above, first read the integers into an array. Then output the array in reverse.integer array(10) userInts

User Ldez
by
8.0k points

1 Answer

2 votes

Final answer:

The program uses a for loop to collect 10 integers into a list, then another for loop with the reversed() function to print them in reverse order. Each integer is followed by a space and the program ends with a newline.

Step-by-step explanation:

To write a program that reads a list of 10 integers and outputs them in reverse, you must first collect the integers into an array. Then, you can iterate over the array in reverse order to output each integer, followed by a space.

Here is an example of how you can write the program in Python:

# Initialize an empty list to store the integers
userInts = []

# Read 10 integers from the user and add them to the list
for _ in range(10):
number = int(input("Enter an integer: "))
userInts.append(number)

# Output the list in reverse
for i in reversed(userInts):
print(i, end=' ')
print() # To output a newline at the end

This program starts by initializing an empty list userInts. It then reads 10 integers from the user using a for loop and the input function. Each integer is converted to an integer from string type using int() and appended to the list. After all integers have been read, another for loop is used with the reversed() function to iterate over the list in reverse order, printing each element followed by a space.

User Volte
by
6.8k points