Write a program to swap the numbers in even position using call by reference
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int n;
cout<<"Enter size of array : ";
cin>>n;
int arr[n];
cout<<"Enter Array elements : ";
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
for(int i=1; i<n; i++)
{
if(i%2 != 0)
{
swap(arr[i], arr[i-1]);
}
}
cout<<"Array after swapping even position elements : ";
for(int i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
}
Comments
Leave a comment