4 Best Ways To Swap Two Numbers in Java without using temporary/Third variable?

4 Best Ways To Swap Two Numbers in Java without using temporary/Third  variable?

In this tutorial, We will discuss how to swap two numbers in Java without using temporary variable.

This is very common interview question for freshers or 1-2 experience.

First we will see how to swap using two numbers with using temporary variable then next without third variable.

Sum of  Two Numbers in Java

4 Best Ways To Swap Two Numbers in Java without using temporary variable?


1. Way 1 Swap With Temp variable:



package com.adeepdrive.swap.numbers;

public class SwapTwoNumbers {
    public static void main(String[] args) {
        int a = 10, b = 20;
        System.out.println("Way 1 Before swaping using temp variable a = " + a + ", b = " + b);
        int temp;
        temp = a;
        a = b;
        b = temp;
      System.out.println("After swaping using temp variable a = " + a + ", b = " + b);
    }
}

Output:

Before swaping using temp variable a = 10, b = 20
After swaping using temp variable a = 20, b = 10


Explanation:

Here, first stored a value in temp variable. Now values will as below.

temp = 10
a = 10
b = 20

Second, we did a = b;

temp = 10
a = 20
b = 20

Then next, b = temp; which temp holds 10, putting into now b varaible.

temp = 10
a = 20
b = 10

That's all.

We will learn now swapping without using Temp variable.

2. Way 2, Way 3 Using '+', '-' operators:



package com.adeepdrive.swap.numbers;

public class SwapTwoNumbers {
    public static void main(String[] args) {
        int a = 10, b = 20;
        System.out.println("Way 2 Before swaping with '+', '-' operators: a = " + a + ", b = " + b);
        a = a + b;
        b = a - b;
        a = a - b;
        System.out.println("After swaping with '+', '-' operators: a = " + a + ", b = " + b);

        int x = 10, y = 20;
        System.out.println("Way 3 Before swaping with '+', '-' operators: a = " + x + ", b = " + y);
        x = x - y;
        y = x + y;
        x = y - x;
        System.out.println("After swaping with '+', '-' operators: a = " + x + ", b = " + y);
    }
}

Swapping two numbers in java.

Output:

Way 2 Before swaping with '+', '-' operators: a = 10, b = 20
After swaping with '+', '-' operators: a = 20, b = 10
Way 3 Before swaping with '+', '-' operators: a = 10, b = 20
After swaping with '+', '-' operators: a = 20, b = 10


a = 10, b = 20
a = a + b; --> a = 30, b = 20
b = a - b; --> a = 30, b = 10
a = a - b; --> a = 20, b  10

x = 10, y = 20
x = x - y; --> x = -10, y = 20
y = x + y; --> x = -10, y = 10
x = y - x; --> x = 20, y = 10

3. Way 4 Using '*', '/' operators:



package com.adeepdrive.swap.numbers;

public class SwapTwoNumbersMultiplyu {
    public static void main(String[] args) {
        int a = 10, b = 20;
        System.out.println("Before swaping with '*', '/' operators: a = " + a + ", b = " + b);
        a = a * b; // a = 200 b = 20
        b = a / b; // a = 200 b = 10
        a = a / b; // a = 20 b = 10
        System.out.println("After swaping with '*', '/' operators: a = " + a + ", b = " + b);
    }
}


Output:

Way 4 Before swaping with '*', '/' operators: a = 10, b = 20
After swaping with '*', '/' operators: a = 20, b = 10

a = 10, b = 20
a = a * b; --> a = 200 b = 20
b = a / b; --> a = 200 b = 10
a = a / b; --> a = 20 b = 10

Please leave your questions in comments section.
       

0 Comments