Java

How to Swap Two Numbers in Java

Are you working with numbers and wondering how to swap two numbers? No worries! This post will facilitate you in this regard. In Java, various approaches can be adapted to swap the two numbers. The most often used method is to utilize a temporary variable. By the end of this article, you will be able to swap two numbers using the below-listed procedures:

Let’s start!

How to Swap Two Numbers Using Third/Temporary Variable

We can exchange/swap the values of two numbers with the help of a third(temporary) variable that will hold the value of these variables temporarily:

import java.util.Scanner;

public class SwapExample {
 public static void main(String[] args) {
     int firstNumber, secondNumber, temp;
     Scanner scan = new Scanner (System.in);
     System.out.println("Enter First Number");
     firstNumber = scan.nextInt();
     System.out.println("Enter Second Number");
     secondNumber = scan.nextInt();
     temp = firstNumber;
     firstNumber = secondNumber;
     secondNumber = temp;
     System.out.println("After Swapping: ");
     System.out.println("First Number: " + firstNumber);
     System.out.println("Second Number: " + secondNumber);
    }
}

The above code snippet performed the following functionalities:

  • Utilized scanner class to take two numbers from the user.
  • Utilized a variable “temp” to store the first variable’s value temporarily.
  • Assigned the value of the second variable(secondNumber) to the first variable(firstNumber).
  • Assigned the value of the “temp” variable to the second variable(secondNumber).

In this way value of both variables will be exchanged, and the following will be the output for the above code snippet:

The above snippet verified that both the numbers swapped successfully.

How to Swap Two Numbers Without Using Third/Temporary Variable

We can swap the two numbers without using a temporary variable, and to do so, we can adopt the below-given procedure:

public class SwapExample {
        public static void main(String[] args) {
            int firstNumber, secondNumber;
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter First Number");
            firstNumber = scan.nextInt();
            System.out.println("Enter Second Number");
            secondNumber = scan.nextInt();
            firstNumber = firstNumber - secondNumber;
            secondNumber = firstNumber + secondNumber;
            firstNumber = secondNumber - firstNumber;
            System.out.println("After Swapping: ");
            System.out.println("First Number: " + firstNumber);
            System.out.println("Second Number: " + secondNumber);
        }
}

Consider the below-listed points to understand the logic of the above-given code:

  • Utilized scanner class to take two numbers from the user (let’s suppose the user entered 14 and 12).
  • Subtract the secondNumber from the firstNumber and assign the resultant value to the firstNumber (e.g., firstNumber = 14 – 12 = 2 ). Now the value of the firstNumber is equal to “2”.
  • Next, add both the numbers and assign the result to the secondNumber (e.g. secondNumber = 2 + 12 = 14).
  • Finally, subtract the firstNumber from the secondNumber and assign the result to the firstNumber (e.g., firstNumber = 14 – 12 = 2 ).

In this way value of both variables will be swapped without using a third variable, and the output for the above code snippet will be:

The above snippet verified the working of the above code.

Conclusion

In java, two numbers can be swapped with or without using a temporary/third variable. In the case of a temporary variable, assign the first variable’s value to the temporary variable and the second variable’s value to the first variable. Next, assign the temporary variable’s value to the second variable. Thereby both numbers will swap. In order to swap two numbers without using a temporary variable, subtract the second number from the first number and assign the result to the first number. Afterward, add both the numbers and assign the result to the second number, finally, subtract the first number from the second number and assign the result to the first number.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.