Swap two variables without using Third Variable in Java

swap two variables without using third variable in java

Swap two variables without using third variable in java

Swapping value of two variable is well know question, and we can do it very easily with the help of third temporary variable. But in which we have to declare a third variable and store value in it. But in this case we have to allocate some memory to that variable. It is not efficient. So we need to find another efficient way to swap two variable without using third variable. 

How to swap two variable without using third variable

So to ‘swap two variable without using third variable in java’ we can do it with the help of arithmetic operators (+, -, +. *).So let’s see the logic of this thing.

Also Read: Different Java Data Type – Primitive and Non-Primitive Data type

Also Read: Different types of Variables in Java – Based on it’s Value Representation and it’s Declaration Position

Program:

  • With the help of plus(+) and minus(-) arithmetic operator swap two variable without using third variable :
public class SwapTwoVariableWitoutThirdVar {

    public static void main(String[] args) {

        int a = 10;
        int b = 20;

        System.out.println("A=" + a);
        System.out.println("B=" + b);

        swap(a, b);

    }

    public static void swap(int a, int b) {
        a = a + b; 
        b = a - b;
        a = a - b; 

        System.out.println("After Swapping");
        System.out.println("A=" + a);
        System.out.println("B=" + b);

    }

}
  • With the help of plus(*) and minus(/) arithmetic operator swap two variable without using third variable :
public class SwapTwoVariableWitoutThirdVar {

    public static void main(String[] args) {

        int a = 10;
        int b = 20;

        System.out.println("A=" + a);
        System.out.println("B=" + b);

        swap(a, b);

    }

    public static void swap(int a, int b) {
        a = a * b; 
        b = a / b; 
        a = a / b; 

        System.out.println("After Swapping");
        System.out.println("A=" + a);
        System.out.println("B=" + b);

    }

}

Download swap two variable without using third variable through Arithmetic Operator,click here.

  • Above both the program will give a same output. A value of variable A and B is 10 and 20 respectively but after execution of this program it will be change and become 20 and 10 of variable A and B respectively. In this program we do not need to use third variable so we doesn’t require for extra memory for it. So this way is more efficient to swap 2 variables without using 3rd variable in java.

Output:

swap two variable without using third variable

I hope now you will get the clear idea of “swap two variables without using third variable in java” easily and now you can done it with your self.

Thank You !

Stay Connected with Place4Java and follow on Facebook.

1 Trackback / Pingback

  1. Java program to count number of vowels in a sentence

Leave a Reply

Your email address will not be published.


*