Read n numbers and print them in reverse order, separated by single spaces.
What to build
An array read forwards and printed backwards.
Requirements
- Read the numbers.
- Print them from the last to the first.
Input
The first line has n (1 ≤ n ≤ 100), the second line n whole numbers.
Output
The numbers in reverse order on one line.
Constraints
n is from 1 to 100.
Example
Input:
4
1 2 3 4
Output:
4 3 2 1
Why it works
Counting i down from n - 1 to 0 visits exactly the valid indexes in reverse order.
Examples
Example 1 · Input
4 1 2 3 4
Expected output
4 3 2 1
Example 2 · Input
1 9
Expected output
9
Hints
Hint 1
Store the numbers in an array first.
Hint 2
Loop from i = n - 1 down to 0.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
int main(void) {
int n, a[100];
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = n - 1; i >= 0; i--) printf("%d%s", a[i], i > 0 ? " " : "\n");
return 0;
}