Reverse Array in C: Print Elements Backwards

One way to reverse the elements in an array is by looping through the array.

  1. Define an integer array and initialize its elements.
  2. Iterate through the elements starting from the last one using a for loop until reaching the first element.
  3. In the loop, use the printf function to output the current element being traversed.

The example code is shown below:

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int length = sizeof(arr) / sizeof(arr[0]);

    for (int i = length - 1; i >= 0; i--) {
        printf("%d ", arr[i]);
    }

    return 0;
}

The output is: 5 4 3 2 1

bannerAds