C++ Program to reverse an array elements


In this program, You will learn how to reverse an array elements in C++.


List is: 10 20 30 40 50 60

After: 60 50 40 30 20 10

Example: How to reverse array elements in C++.

#include<iostream>
using namespace std;

int main() {

    int arr[100];
    int i, j, s, temp;

    cout << "Enter size of an array:";
    cin>>s;

    cout << "Enter array elements:";
    for (i = 0; i < s; i++) {
        cin >> arr[i];
    }

    j = s - 1;
    for (i = 0; i < j; i++) {
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        j--;
    }

    cout << "After reverse array elements:";
    for (i = 0; i < s; i++) {
        cout << arr[i] << " ";
    }

    return 0;
}

Output:

Enter size of an array:5
Enter array elements:10 20 30 40 50
After reverse array elements:50 40 30 20 10