C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Arrays in C

Reverse array elements

C Program: Reverse array elements

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i, temp;

 

    // Input array size

    printf("Enter number of elements in the array: ");

    scanf("%d", &n);

 

    // Input array elements

    printf("Enter %d elements:\n", n);

    for (i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }

 

    // Reverse the array

    for (i = 0; i < n / 2; i++) {

        temp = arr[i];

        arr[i] = arr[n - i - 1];

        arr[n - i - 1] = temp;

    }

 

    // Display reversed array

    printf("\nReversed array elements:\n");

    for (i = 0; i < n; i++) {

        printf("%d ", arr[i]);

    }

 

    printf("\n");

    return 0;

}

Output

 
INPUT :
Enter number of elements in the array: 5
Enter 5 elements:
10 20 30 40 50


OUTPUT :
Reversed array elements:
50 40 30 20 10


Explanation

  1. The program reads the number of elements and their values into arr.
  2. A loop swaps the first element with the last, the second with the second-last, and so on — until the midpoint.
  3. The reversed array is then displayed.