Swap the contents of two variables using a third variable.
#include <iostream>
using namespace std;
int main() {
int x = 10;
int y = 5;
int temp;
cout << "The first variable \'x\' value is: " << x << endl;
cout << "The second variable \'y\' value is: " << y << endl;
// Swapping the contents of two variables using a third variable
temp = x;
x = y;
y = temp;
cout << "\nWe have just swapped the contents of two variables using a third variable:" << endl;
cout << "The first variable \'x\' value is: " << x << endl;
cout << "The second variable \'y\' value is: " << y << endl;
return 0;
}
Comments
Leave a comment