Swap three variables without using temporary variable | C ++ program to swap three variables without using temporary variable ; Assign c ' s ▁ value ▁ to ▁ a , ▁ a ' s value to b and b 's value to c. ; Store XOR of all in a ; After this , b has value of a ; After this , c has value of b ; After this , a has value of c ; Driver code ; Calling Function
#include <iostream> NEW_LINE using namespace std ; void swapThree ( int & a , int & b , int & c ) { a = a ^ b ^ c ; b = a ^ b ^ c ; c = a ^ b ^ c ; a = a ^ b ^ c ; } int main ( ) { int a = 10 , b = 20 , c = 30 ; cout << " Before ▁ swapping ▁ a ▁ = ▁ " << a << " , ▁ b ▁ = ▁ " << b << " , ▁ c ▁ = ▁ " << c << endl ; swapThree ( a , b , c ) ; cout << " After ▁ swapping ▁ a ▁ = ▁ " << a << " , ▁ b ▁ = ▁ " << b << " , ▁ c ▁ = ▁ " << c << endl ; return 0 ; }