C++ Programs Examples
How to swap two variables in c++? We can swap two variables very easily in c++. There are two methods of swap. First method: By using a temporary variable. Second method: Without using temporary variable. Lets start with the first method. We use a third variable which is called temporary variable and represented as temp. The code is very simple and is given below: #include<iostream> using namespace std; void main() { int a = 5, b = 10; cout << "a= " << a << " " << "b= " << b << endl; int temp = a; a = b; b = temp; cout << "After swap:" << endl; cout << "a= " << a << " " << "b= " << b << endl; system("pause"); }