Example: #1 - Swapping of two numbers using temporary variable
C++ Input Screen
#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
int a = 3;
int b = 6;
cout << "The value of a before swap is " << a << endl;
cout << "The value of b before swap is " << b << endl;
// Swapping of two numbers using temp variable
int temp = a;
a = b;
b = temp;
cout << "The value of a after swap is " << a << endl;
cout << "The value of b after swap is " << b << endl;
return 0;
}
C++ Output Screen
The value of a before swap is 3
The value of b before swap is 6
The value of a after swap is 6
The value of b after swap is 3