15,614,138 members
Articles / Programming Languages / Java
Alternative
Tip/Trick
Posted 17 Feb 2012

18.3K views

# Swap Two Numbers without using Temp Variable

Rate me:
17 Feb 2012CPOL
That conclusion is definitely wrong. Your equation expanded equals: b = (a+b) - (a-b) = a+b - a + b = 2*bFor your example a=20, b=10b = (20+10) - (20-10) = (30) - (10) = 20b = 20 + 10 - 20 + 10 = 20A counter example to disprove this: a=75, b=88b = (75+88) - (75-88) =...
That conclusion is definitely wrong.

b = (a+b) - (a-b) = a+b - a + b = 2*b

b = (20+10) - (20-10) = (30) - (10) = 20
b = 20 + 10 - 20 + 10 = 20

A counter example to disprove this:
a=75, b=88

b = (75+88) - (75-88) = (163) - (-13) = 163 + 13 = 176

this equals the expanded algebraic expression :
b = (a+b) - (a-b) = a+b - a + b = 2*b
b = 75 + 88 - 75 + 88 = 88*2 = 176

It appears it was just coincidental that your example worked, not a mathematical truth. Good work trying to find a mathemagical short cut though.

Written By
United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.