Assignments » Single Dimension Array » Set1 »Solution 2

Write a C++ program to swap first and last element of an integer 1-d array.

#include<iostream>
using namespace std;

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

	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];
	}

	temp=Arr[0];
	Arr[0]=Arr[n-1];

	Arr[n-1]=temp;

	cout<<"\nArray after swapping"<<endl;

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

	return 0;
}

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

Array after swapping
20 69 30 51 13