Swapping two numbers without temporary variable in Java

In this article, we will discuss one of the famous interview question on how to swap two numbers without using third/temporary variable

Actually, there are number of tricks used by programmer to achieve swap program but we will limit for the following cases,

 

Swapping 2 numbers:

  1. Using addition-subtraction method
  2. Using division-multiplication method
  3. Using third variable – this is actually to understand

 

Example 1: using addition-subtraction method

Swap2NumbersUsingAddSubMethod.java

package in.bench.resources.swap.numbers;

public class Swap2NumbersUsingAddSubMethod {

	public static void main(String[] args) {

		int firstNum = 500;
		int secondNum = 700;

		// original values before swap
		System.out.println("Before swap of 2 numbers :");
		System.out.println("First Number  : " + firstNum);
		System.out.println("Second Number : " + secondNum);

		// swapping 2 numbers using Addition & Subtraction
		firstNum = firstNum + secondNum; // 1n=1200, 2n=700
		secondNum = firstNum - secondNum; // 1n=1200, 2n=500
		firstNum = firstNum - secondNum; // 1n=700, 2n=500

		// original values before swap
		System.out.println("\nAfter swap of 2 numbers :");
		System.out.println("First Number  : " + firstNum);
		System.out.println("Second Number : " + secondNum);
	}
}

Output:

Before swap of 2 numbers :
First Number  : 500
Second Number : 700

After swap of 2 numbers :
First Number  : 700
Second Number : 500

 

Example 2: using division-multiplication method

Swap2NumbersUsingDivMulMethod.java

package in.bench.resources.swap.numbers;

public class Swap2NumbersUsingDivMulMethod {

	public static void main(String[] args) {

		int firstNum = 10;
		int secondNum = 15;

		// original values before swap
		System.out.println("Before swap of 2 numbers :");
		System.out.println("First Number  : " + firstNum);
		System.out.println("Second Number : " + secondNum);

		// swapping 2 numbers using Multiplication & Division
		firstNum = firstNum * secondNum; // 1n=150, 2n=15
		secondNum = firstNum / secondNum; // 1n=150, 2n=10
		firstNum = firstNum / secondNum; // 1n=15, 2n=10

		// original values before swap
		System.out.println("\nAfter swap of 2 numbers :");
		System.out.println("First Number  : " + firstNum);
		System.out.println("Second Number : " + secondNum);
	}
}

Output:

Before swap of 2 numbers :
First Number  : 10
Second Number : 15

After swap of 2 numbers :
First Number  : 15
Second Number : 10

 

Example 3: using third/temporary variable

Swap2NumbersUsingThirdVariable.java

package in.bench.resources.swap.numbers;

public class Swap2NumbersUsingThirdVariable {

	public static void main(String[] args) {

		int firstNum = 33;
		int secondNum = 66;
		int iTempVar;

		// original values before swap
		System.out.println("Before swap of 2 numbers :");
		System.out.println("First Number  : " + firstNum);
		System.out.println("Second Number : " + secondNum);

		// swapping 2 numbers using third/temporary variable
		iTempVar = firstNum; // 1n=33, 2n=66, tn=33
		firstNum = secondNum; // 1n=66, 2n=66, tn=33
		secondNum = iTempVar; // 1n=66, 2n=33, tn=33

		// original values before swap
		System.out.println("\nAfter swap of 2 numbers :");
		System.out.println("First Number  : " + firstNum);
		System.out.println("Second Number : " + secondNum);
	}
}

Output:

Before swap of 2 numbers :
First Number  : 33
Second Number : 66

After swap of 2 numbers :
First Number  : 66
Second Number : 33

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

Happy Coding !!
Happy Learning !!