Programming Examples
Cpp program to reverse an array
Write a program to reverse an array
Solution
#include<iostream>
using namespace std;
int main()
{
int arr[50],size,i,j;
cout<<" Enter the size of array";
cin>>size;
cout<<"Enter the elements in this array";
for(i=0;i<size;i++)
{
cin>>arr[i];
}
for(i=0,j=size-1;i<j;i++,j--)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
cout<<"Reversed array :"<<endl;
for(i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
Output
Enter the size of array
9
Enter the elements in this array
1
2
3
4
0
5
6
7
8
Reversed array :
8 7 6 5 0 4 3 2 1