Solution Review: Swap the Values of Two Variables
Let's see the detailed solution review of the challenge given in the previous lesson.
We'll cover the following...
Solution #
Press + to interact
#include <iostream>using namespace std;int main() {// Initialize variables var1, var2 and tempint var1 = 10;int var2 = 20;int temp;// Print the values of var1 and var2cout << "Initial values of var1 and var2 are:" << endl;cout << "var1 = " << var1 << endl;cout << "var2 = " << var2 << endl;// Stores value of var1 in temptemp = var1;// Stores value of var2 in var1var1 = var2;// Stores value of temp in var2var2 = temp;cout << "After swapping:" << endl;cout << "var1 = " << var1 << endl;cout << "var2 = " << var2;}