Computer Science, asked by narendramodi5087, 11 months ago

Program to swap first and last element of an integer 1-d array c++

Answers

Answered by ParvezShere
9

#include<iostream>

using namespace std;

int main()

{

int n, s,i;

cout<<"Enter Number of elements you want to enter : ";

cin>>n;

int ar[n];

cout<<"Enter the elements of the array "<<endl;

for( i = 0;i<n;i++)//loop to input elements from user

{

cin>>ar[i];

}

// Now swapping the first and last element of the array using swap metheod.

s = ar[0];

ar[0] = ar[n-1];

ar[n-1] = s;

cout<<"Array after swapping first and last elements "<<endl;

for(i = 0;i<n;i++) // loop to display the elements after swapping

{

cout<<ar[i]<<endl;

}

return 0;

}

Similar questions