Click here to Skip to main content
15,881,381 members
Articles / Programming Languages / Java
Alternative
Tip/Trick

Swap Two Numbers without using Temp Variable

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
17 Feb 2012CPOL 19.2K   2
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.

Your equation expanded equals:
b = (a+b) - (a-b) = a+b - a + b = 2*b

For your example a=20, b=10
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions

 
GeneralReason for my vote of 5 Great!!! Pin
Mahdi 8216102118-Feb-12 3:39
Mahdi 8216102118-Feb-12 3:39 
Generalmy logic was this one.., some one has updated that wrongly..... Pin
The Doer17-Feb-12 17:02
The Doer17-Feb-12 17:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.