In: Computer Science
#include<iostream>
using namespace std;
//Function swap to swap the variables. Call by reference is used so that swapping occurs in the original variables and not just //the local variables.
void swap(int &var1, int &var2)
{
int temp;
temp = var1;
var1 = var2;
var2 = temp;
}
int main()
{
int variable1, variable2;
cout<<"Enter 2 integers to be
swapped"<<endl;
cin>>variable1>>variable2;
//Values before swapping
cout<<"Values before
swapping:"<<endl<<"variable1:
"<<variable1<<endl<<"variable2:
"<<variable2<<endl;
swap(variable1, variable2); //Function call
//Values after swapping
cout<<"Values after swapping:"<<endl<<"variable1:
"<<variable1<<endl<<"variable2:
"<<variable2<<endl;
}
Output:
hope you like it
please provide comments if you have any doubts