How do you swap two variables A and B without using a
3rd variable? No function or method allowed.
For example:
Given:
A = 35
B = 10
Result
A = 10
B = 35
A = A + B (A = 10 + 35 = 45)
B = A - B (B = 45 - 35 = 10)
A = A - B (A = 45 - 10 = 35)
A = A + B (A = 35 + 10 = 45)
B = A - B (B = 45 - 10 = 35)
A = A - B (A = 45 - 35 = 10)
if I read that response correctly, you end up withThanks for correciting me... I just took the numbers for example... the end result is same what is expected... Thanks anyways :-)
the same thing you started with. should be:A = A + B (A = 35 + 10 = 45) B = A - B (B = 45 - 10 = 35) A = A - B (A = 45 - 35 = 10)
just out of curiousityForget it, its a pointless exercise. You would never ever do this in practice.
A=(A^=B)^(B^=A);