Swap two numbers without using third variable.
A = B / A (now, A will have value val b/ val a and B will have value val b)
B = B / A (now, A will have value val b/ val a and B will have value val a)
A = A * B (now, A will have value val b and B will have value val a)
Above method overflow should be take care
A = A + B;
B = A – B;
A = A – B;
A = A ^ B;
B = A ^ B;
A = A ^ B;
Comments