Gadgets

Monday, 23 November 2015

Swapping two numbers without using third variable

Swapping two numbers without using third variable
 Type one:
   public class Swapping {
    public static void main(String[] arg){
        int a=100,b=200;
        a=a+b;
        b=a-b;
        a=a-b;
        System.out.println("a value is : "+a+" b value is :"+b);
    }   
}
Output:
a value is : 200 b value is :100


Type two:
public class Swapping {  
    public static void main(String[] arg){
        int a=57,b=67;
        a=a*b;
        b=a/b;
        a=a/b;
        System.out.println("a value is : "+a+" b value is :"+b);
    }   
}
Output:
a value is : 67 b value is :57

No comments:

Post a Comment