Swap the content of two variables without using a third variable.
It can be achieved by two methods one is by Arithmetic method and the other is by Using Bitwise XOR.
I am doing it by using Arithmetic operators-
#include <iostream>
using namespace std;
int main()
{
int x = 10, y = 50;
x = x + y; // x = 60
y = x - y; // y = 10
x = x - y; // x = 50
cout << "After Swapping: x = " << x << ", y = " << y;
}
Comments
Leave a comment