Challenge: Reverse the List
Completed
Read n, then n whole numbers. Print them in reverse order, separated by spaces.
Input
n (1 ≤ n ≤ 100), then n integers.
Output
The numbers in reverse order on one line.
Example
Input
5 1 2 3 4 5
Output
5 4 3 2 1
Editor
C
Hints
Store the numbers in an array first.
Then loop from index n - 1 down to 0.
Solution explanation
Once all numbers are stored, a loop that counts down visits them from last to first.
Show solution
#include <stdio.h>
int main() {
int n;
int nums[100];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &nums[i]);
}
for (int i = n - 1; i >= 0; i--) {
printf("%d", nums[i]);
if (i > 0) {
printf(" ");
}
}
printf("\n");
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module