C++ Program to print array in reverse order


C++ Program to print array in reverse order

        The array is one of the most asked topics in any coding exam. Hence one must know how to read and access array elements. The below is a basic program that explains how to read array elements and how to access and print them in reverse order.


Code

#include <iostream>

using namespace std;


int main()

{

    int n;                              //array size

    cin>>n;

    

    int a[n];                       //array declaration

    

    for(int i=0;i<n;i++){           //reading array

        cin>>a[i];

    }

    

    for(int i=n-1;i>=0;i--){        //printing array in reverse

        cout<<a[i]<<" ";

    }


    return 0;

}


Input:

5

1 2 3 4 5

Output:

5 4 3 2 1

Post a Comment

0 Comments