165k views
5 votes
Write a program that reads integers from the user and stores them in a list. Use 0 as a sentinel value to mark the end of the input. Once all of the values have been read your program should display them (except for the 0) in reverse order, with one value appearing on each line.

1 Answer

4 votes

Answer:

The program is written in Python:

num = int(input("User Input: "))

mylist = []

while(num !=0):

mylist.append(num)

num = int(input("User Input: "))

mylist.reverse()

for i in mylist:

print(i,end = ' ')

Step-by-step explanation:

This question is answered using Python and it uses 0 as a sentinel value; i.e. the program stops prompting user for input when user enters 0

This line prompts user for input

num = int(input("User Input: "))

This line declares an empty list

mylist = []

This loop is repeated as long as user enters input other than 0

while(num !=0):

This appends user input to the list

mylist.append(num)

This prompts user for another input

num = int(input("User Input: "))

This reverses the list

mylist.reverse()

The following iteration the list in reverse order

for i in mylist:

print(i,end = ' ')

User Greenkode
by
4.2k points