English

Write a program to input n numbers and reverse the set of numbers without using functions.

Advertisements
Advertisements

Question

Write a program to input n numbers and reverse the set of numbers without using functions.

Code Writing
Advertisements

Solution

A set is unordered, so it cannot be reversed meaningfully. Therefore, use a list of numbers and print its elements from the last index to the first index without using the reverse() function.

n = int(input("Enter number of elements: "))
numbers = []

for i in range(n):
    number = int(input("Enter a number: "))
    numbers.append(number)

print("Numbers in reverse order:")
i = n - 1
while i >= 0:
    print(numbers[i], end=" ")
    i -= 1
 
shaalaa.com
  Is there an error in this question or solution?
Chapter 1: Review of Phython - EXERCISE [Page 27]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 1 Review of Phython
EXERCISE | Q 32. | Page 27
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×