Write a C program to print the elements of an array in reverse order
Description: Take 5 numbers in an array and print them in reverse order.
Hint:
- Start the loop from the last index (size – 1) and go down to 0.
C program to print an array in reverse order
#include <stdio.h>
int main(void) {
int a[5];
printf("Enter 5 numbers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &a[i]);
}
printf("Reverse order: ");
for (int i = 4; i >= 0; i--) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
Output:
Output of the above program is
Enter 5 numbers: 1
2
3
4
5
Reverse order: 5 4 3 2 1
How it works
- The second loop starts at index 4 and goes down to index 0.
- The array itself is not changed. We only print it in reverse.
Learn more about the similar topics:
| Tutorials |
|---|
| C Arrays |
| C Two Dimensional Arrays |
| C Passing Arrays to Functions |
| C Strings |
| C String Input and Output |
| C String Functions |
| C strlen() Function |
| Interview Questions & Answers |
|---|
| What is an array in C? |
| How is a string stored in C? |
| How to reverse a string in C? |
| What is the difference between memcpy() and strcpy() in C? |