C++ Program To Swap Two Numbers
Write C++ code to swap two numbers using different methods
// Write C++ code to swap two numbers using different methods #include <bits/stdc++.h> using namespace std ; int main() { int a = 45 , b = 54, temp; cout << "Method - 1: \n"; cout << "Before swapping " << a <<" and "<< b <<endl ; // METHOD - 1 temp = a; a = b; b = temp; cout << "After swapping " << a <<" and "<< b <<endl ; // METHOD - 2 a = 44; b = 55; cout << "\nMethod - 2: \n"; cout << "Before swapping " << a <<" and "<< b <<endl ; a = a + b; b = a - b; a = a - b; cout << "After swapping " << a <<" and "<< b <<endl ; return 0 ; }
Output:
Method - 1:
Before swapping 45 and 54
After swapping 54 and 45
Method - 2:
Before swapping 44 and 55
After swapping 55 and 44