How to swap two numbers without using temporary variable – in Java?

Swapping of two numbers without the third or temporary variable is one of the most popular interview question asked, most of the freshers failed to solve without the third variable. Lets now take a look into the possible approaches.

Method 1 : Using Addition and Subtraction

The value of a and b can be swapped, by adding and subtraction from the sum.

package com.javainterviewpoint;

public class SwappingNumbers 
{
    public static void main(String args[])
    {
        int a = 5;
        int b =10;
        System.out.println("***Before Swapping***");
        System.out.println("a value: "+a);
        System.out.println("b value: "+b);
        a = a + b; //a is now 15
        b = a - b; //b is now 10
        a = a - b; //a is now 5
        System.out.println("***After Swapping***");
        System.out.println("a value: "+a);
        System.out.println("b value: "+b);
    }
}

Output :

***Before Swapping***
a value: 5
b value: 10
***After Swapping***
a value: 10
b value: 5

Method 2 : Using Multiplication and Division

Similar to previous method, we can do swapping using multiplication and division. Multiplying both numbers and dividing the multiplied value.

package com.javainterviewpoint;

public class SwappingNumbers 
{
    public static void main(String args[])
    {
        int a = 5;
        int b =10;
        System.out.println("***Before Swapping***");
        System.out.println("a value: "+a);
        System.out.println("b value: "+b);
        a = a * b; //a is now 50
        b = a / b; //b is now 5
        a = a / b; //a is now 10
        System.out.println("***After Swapping***");
        System.out.println("a value: "+a);
        System.out.println("b value: "+b);
    }
}

Output :

***Before Swapping***
a value: 5
b value: 10
***After Swapping***
a value: 10
b value: 5

Method 3 : Using Bitwise XOR operator

Bitwise XOR Operator can be used to swap two variables.

package com.javainterviewpoint;

public class SwappingNumbers 
{
    public static void main(String args[])
    {
        int a = 5;
        int b =10;
        System.out.println("***Before Swapping***");
        System.out.println("a value: "+a);
        System.out.println("b value: "+b);
        a = a ^ b; //a is now 15(1111)
        b = a ^ b; //b is now 10(1010)
        a = a ^ b; //a is now 5(0101)
        System.out.println("***After Swapping***");
        System.out.println("a value: "+a);
        System.out.println("b value: "+b);
    }
}

Output :

***Before Swapping***
a value: 5
b value: 10
***After Swapping***
a value: 10
b value: 5

Method 4 : Simplest of All

This is the simplest method of all, the logic will be made in a single line ” b = a – b+(a =b) “

package com.javainterviewpoint;

public class SwappingNumbers 
{
    public static void main(String args[])
    {
        int a = 5;
        int b =10;
        System.out.println("***Before Swapping***");
        System.out.println("a value: "+a);
        System.out.println("b value: "+b);
        /**
         * Here a - b will be -5
         * a = b ==> a is 10 now
         * Finally adding b will be 5
         */
        b = a - b + (a = b);
        System.out.println("***After Swapping***");
        System.out.println("a value: "+a);
        System.out.println("b value: "+b);
    }
}

Output :

***Before Swapping***
a value: 5
b value: 10
***After Swapping***
a value: 10
b value: 5

Leave a Reply

Your email address will not be published. Required fields are marked *