Assignments » Single Dimension Array » Set1 »Solution 3

Write a C++ program to reverse the element of an integer 1-D array.

#include<iostream>
using namespace std;

int main()
{
	int Arr[100],n,temp,i,j;

	cout<<"Enter number of elements you want to insert ";
	cin>>n;

	for(i=0;i<n;i++)
	{
		cout<<"Enter element "<<i+1<<":";
		cin>>Arr[i];
	}

	for(i=0,j=n-1;i<n/2;i++,j--)
	{
		temp=Arr[i];
		Arr[i]=Arr[j];
		Arr[j]=temp;
	}

	cout<<"\nReverse array"<<endl;

	for(i=0;i<n;i++)
		cout<<Arr[i]<<" ";

	return 0;
}


Enter number of elements you want to insert 6
Enter element 1: 13
Enter element 2: 69
Enter element 3: 30
Enter element 4: 51
Enter element 5: 11
Enter element 6: 81

Reverse array
81 11 51 30 69 13