C++ Program:

- To swap 2 numbers using XOR operator

Example:

User Input
1st number: 3
2nd number: 5

After Swap, Output:
1st number : 5
2nd number : 3

#include <iostream>
using namespace std;

int main() {
  int n1,n2;
  cout<<"Enter 1st number: ";
  cin>>n1;
  cout<<"Enter 2nd number: ";
  cin>>n2;
  n1^=n2;
  n2^=n1;
  n1^=n2;
  cout<<"After Swap"<<endl;
  cout<<"1st number :"<<n1<<"\n2nd number :"<<n2;
  return 0;
}

Output:

  Enter 1st number : 7
  Enter 2nd number : 15
  After Swap
  1st number : 15
  2nd number : 7
Program
puzzle

We are using binary XOR operator (^)

//Lets take n1=7, n2=3
n1^=n2; //Here n1=4, n2=3
n2^=n1; //Here n1=4, n2=7
n1^=n2; //Finally n1=3, n2=7
Remember :
 A B A^B
 0 0  0
 0 1  1
 1 0  1
 1 1  0
Concept

Coming Soon !

Quick
Tutorial

Quote

Find a place inside where there’s joy, and the joy will burn out the pain.