Loading

Program for swapping two numbers using a third variable:                                                      

package quipoin.javaeasyprograms;
public class SwapTwoNumber {
     static void swap(int x,int y){
        System.out.println("Element before Swapping:"+x+" "+y);
        int temp=0;
        temp=x;
        x=y;
        y=temp;
        System.out.println("Element after Swapping:"+x+" "+y);
    }
    public static void main(String[] args) {
        swap(78,118);
    }
}

Output:

Element before Swapping:78 118
Element after Swapping:118 78

Program for swapping two numbers without using a third variable:

package quipoin.javaeasyprograms;
 public class SwapTwoNumber {

    static void swap(int a,int b){
        System.out.println("Element before Swapping:"+a+" "+b);
        a=(a+b);
        b=(a-b);
        a=(a-b);
        System.out.println("Element after Swapping:"+a+" "+b);
    }
    public static void main(String[] args) {
        swap(78,118);
    }
}

Output:

Element before Swapping:78 118
Element after Swapping:118 78