SWAP

Muthumari_B · updated September 17, 2021 · C++
#include<iostream>  
using namespace std;
void swap(int*,int*);
void swap(int c,int d);
int main()   
{    
 int a,b;
 cout<<"swapping two numbers using call by referance ";
 cout<<"Enter Two Numbers : ";
 cin>>a>>b;
 cout<<a<<" ,"<<b<<endl;   
 cout<<"Before Swapping;"<<endl;
 cout<<"Value of a is: "<<a<<endl;  
 cout<<"Value of b is: "<<b<<endl; 
 swap(&a,&b);  //call by reference 
 cout<<"After Swapping;"<<endl;
 cout<<"Value of a is: "<<a<<endl;  
 cout<<"Value of b is: "<<b<<endl;  
 cout<<"Before swapping;"<<endl;
 cout<<"Value of a is: "<<a<<endl;  
 cout<<"Value of b is: "<<b<<endl;
 swap(a,b); //call by value
 cout<<"After Swapping ;"<<endl;
 cout<<"Value of a is: "<<a<<endl;
 cout<<"Value of b is: "<<b<<endl;  
 cin>>a,b;
 return 0;  
}
void swap(int *a, int *b)  
{  
 int temp;  
 temp=*a;  
 *a=*b;  
 *b=temp;  
}
void swap(int c, int d)  
{  
 int temp;  
 temp=c;  
 c=d;  
 d=temp;  
}

Comments

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