xxxxxxxxxx
int num1, num2, temp;
cout << " Input 1st number : ";
cin >> num1 ;
cout << " Input 2nd number : ";
cin >> num2;
num2=num2+num1;
num1=num2-num1;
num2=num2-num1;
cout << " After swapping the 1st number is : "<< num1 <<"\n" ;
cout << " After swapping the 2nd number is : "<< num2 <<"\n\n" ;
xxxxxxxxxx
#include <bits/stdc++.h>
// OR
#include <algorithm> // if on C++98
#include <utility> // if on C++11
int a = 2, b = 5;
swap(a, b);
vector<int> v = { 7, 5, 16, 8 }; // #include <vector>
swap(v[0], v[2]);
xxxxxxxxxx
//Author: Subodh
//! Swap two number using XOR operation
cout << "Before, n1 = " << num1 << ", n2 = " << num2 << endl;
num1 = num1 ^ num2, num2 = num1 ^ num2, num1 = num1 ^ num2;
cout << "After, n1 = " << num1 << ", n2 = " << num2 << endl;