Answer:
Following are the program in c++ language
#include<iostream> // header file
using namespace std; // using namespace
void swapvalues(int& userVal1, int& userVal2);// fucnction declaration
int main() // main method
{
int x,y; // variable declaration
cout<<"Enter the two value before swapping:\n";
cin>>x>>y; // read the two input
cout<<"before swapping:";
cout<<x<<y; // display the value before swapping
swapvalues(x, y);
cout << "\nAfter swapping:";
cout<<x<<y; // display the value after swapping
return 0;
}
void swapvalues(int& userVal1, int& userVal2) // function definition
{
int t; // variable declaration
t = userVal1; // assign the value userval1 to variable t
userVal1 = userVal2; // assign the value userval2 to userval1
userVal2 = t; // assign the value of variable t to userval2
}
Output:
Enter the two value before swapping:
3
8
before swapping:3 8
After swapping :8 3
Explanation:
Following are the description of the program
- Declared a variable "x" and "y" of type "int".
- Read a input by user in x and y .
- Display the values of variable x and y before the swapping.
- Call the function swapvalues() by passing the value of x and y.
- After calling the console moved In the definition of swapvalues() Function where it interchanged the value of variable userVal1 and userVal2 by using third variable "t".
- After interchanged the console moved to the main function and finally display the value of variable "x" and "y' after the swapping.