Header Ads

Write a Program in C++ To Swap First Element with Last Element and Second Element to Second Last Element and So On . Swap in Revers Order

In This  Program To Swap First Element with Last Element and Second Element to Second Last Element and So On . Swap in Revers Order using C++ Programming 


#include<iostream>

using namespace std;

//Swap Array Function

void SwapArr(int *arr, int num){

int i,temp;

i = temp = 0;

for(i=0; i<num/2; i++)

{

temp = arr[i];

arr[i] = arr[num-i-1];

arr[num-i-1] = temp;

}

}

// main function

main(){

int arr[25] = {0};

int i , n;

i = n = 0;

cout<<"Enter The Size of Array:";

cin>>n;

cout<<"Enter Elements of Array"<<endl;

for(i =0; i<n; i++){

cout<<"Enter "<<i+1<<" Element:";

cin>>arr[i];

}

cout<<"The Array Befor Swap"<<endl;

for(i =0; i<n; i++)

cout<<arr[i]<<"\t";

SwapArr(arr,n);

cout<<"\nThe Array After Swap"<<endl;

for(i =0; i<n; i++)

cout<<arr[i]<<"\t";

}


Output:


Swap ,array, Swap array, 
Swap First Element with Last Element, Swap First Element with Last Element in C++,
Second Element to Second Last Element, Second Element to Second Last Element in c++,
C++ To Swap First Element with Last Element, C++ To Swap First Element with Last Element and Second Element


No comments

Powered by Blogger.