Home
»
» Unlabelled
» C++ Program Swap First and Last Element of 1-d array
Write C++ program to swap first and last element of an integer 1-d array.
Source Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| #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;
}
|
Hello world
Post a Comment