Reverse an Array
How to Reverse an Array​
Example :
#include <iostream>
using namespace std;
int main() {
int arr[5];
cout<<"Enter 5 Values: ";
for(int i=0; i<5; i++)
{
cin>>arr[i];
}
cout<<"\n Values are: "<<arr[i];
for(int i=4; i>=0; i--)
{
cout<<arr[i]<<", ";
}
return 0;
}
Output :
Explanation :
Initialized array that name has arr
, size of is 5, datatype is int
.
Enter 5 Values:
using cout.
we use for loop for(int i=0; i<5; i++)
and check condition and increase value by one.
arr[i]
using cin
object.
Values are :
and arr[i]
using cout object.
we use for loop for reverse array for(int i=4; i>=0; i--)
check conditon and decrease by one.
Print value of arr[i]
and ,
using cout
.