Swapping four variables without temporary variable | CPP program to swap 4 variables without using temporary variable . ; swapping a and b variables ; swapping b and c variables ; swapping c and d variables ; Driver code ; initialising variables ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void swap ( int a , int b , int c , int d ) { a = a + b ; b = a - b ; a = a - b ; b = b + c ; c = b - c ; b = b - c ; c = c + d ; d = c - d ; c = c - d ; cout << " values ▁ after ▁ swapping ▁ are ▁ : ▁ " << endl ; cout << " a ▁ = ▁ " << a << endl ; cout << " b ▁ = ▁ " << b << endl ; cout << " c ▁ = ▁ " << c << endl ; cout << " d ▁ = ▁ " << d << endl ; } int main ( ) { int a = 1 ; int b = 2 ; int c = 3 ; int d = 4 ; cout << " Values ▁ before ▁ swapping ▁ are ▁ : " << endl ; cout << " a ▁ = ▁ " << a << endl ; cout << " b ▁ = ▁ " << b << endl ; cout << " c ▁ = ▁ " << c << endl ; cout << " d ▁ = ▁ " << d << endl << endl ; swap ( a , b , c , d ) ; return 0 ; }