SWAPPING

Muthumari_B · updated September 07, 2021 · C++
// Swapping two values using call by value and call by reference 


#include<iostream>  
using namespace std;    
void swap(int *p, int *q)  
{  
 int swap;  
 swap=*p;  
 *p=*q;  
 *q=swap;  
}  
int main()   
{    
 int p,q;
cout<<"\nEnter two values : \n";
cin>>p>>q;
cout<<p;
cout<<"\n";
cout<<q;
cout<<"\n Before Swapping";
cout<<"\n p : "<< p <<"\n q : " << q;
cout<<"\n After Swapping";
swap(&p, &q);  
cout<<"\n p : "<< p <<"\n q : " << q;  
return 0;  
}

Comments

Please sign up or log in to contribute to the discussion.