/* Lab : Lab2 Day : Tuesday [ 05 Aug 08 ] Problem Number: 02 Problem Title : Swapping without temporary variable Theory : What swapping means ? swapping means excnange of values between two variable for example if two integer variable a and b have following values a = 20 and b = 10 then after swapping values of a and b becoms as a = 10 and b = 20 How to do it ? well it's simple if we are allowed to use third variable let third variable is t, then following three setps will do the swapping step 1: t = a step 2: a = b step 3: b = t Our original problem do not allow us to use third variable please note that in above solution value of a and b are changed in step 2 and 3. in step 2, on assignment of value b to a the original value of a is lost and a gets new value equal to b it is a crutial step we have to preserve some information so that the value of a can be generated at later time when assigning any value to a in step 2 Please think in this direction. There can be many ways to preserve this information. One of my suggestion is to assign sum of a and b, to a step 1: a = a + b step 2: b = a - b step 3: a = a - b Note : Try to re-write the code by using difference */ class sol_lab2_prob02{ public static void main(String args[]) { int a, b; a = 10; b = 20; System.out.println("Values before swapping"); System.out.println(" a = "+ a +", b = "+ b); //main swapping steps a = a + b; b = a - b; a = a - b; System.out.println("Values after swapping"); System.out.println(" a = "+ a +", b = "+ b); } }