View Single Post
  #6 (permalink)  
Old 04-27-2007, 03:03 PM
goldhouse goldhouse is offline
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
Swapping Algorithm
This is one of the tricky question often asked in interviews.
The result for the below will be
Code:
int a =5, b =8; temp = a; a = b; b = temp; System.out.println("a = " + a + " b = " + b) // give result a=8 b =5
The above code swapped the variable values , Now the question is how can we achieve this without using the temp.

Code:
int double =5,double = 8; a = a + b; b = a -b; a = (a - b) /2 ; System.out.println("a = " + a + " b = " + b) // give result a=8 b =5
I like somebody to explain the below code.
Reply With Quote