Java Program to Swap Two Numbers

1. Introduction


In this article, You'll learn how to swap two numbers in java. Basically, this can be achieved by using a temporary variable and without using the third variable.

First, let us see how to swap two numbers. This is a basic program for engineering students.

You can find more programs as part of programming series.

2. Swapping two Numbers with the third variable

[package com.javaprogramto.numbers.swap;
public class SwapNumbersTemp {

public static void main(String[] args) {

int a = 10;
int b = 20;

System.out.println("Before swapping two numbers : a " + a + ", b " + b);

int temp = a;
a = b;
b = temp;

System.out.println("After swapping two numbers : a " + a + ", b " + b); }
}]

Output:

[Before swapping two numbers : a 10, b 20
After swapping two numbers : a 20, b 10]

As you have seen in the above program, first created two numbers named a and b.

Next, Created a temp variable int type and the stored value of a into temp.

[temp = 10]

Then, storing b value into variable a.

[a = 20]

Finally, storing the temp value into b.

[b = 10]

At the end, printed swapped numbers using the println() method. You can observe the program and output.

Understand each statement in the program that gives a better grip on the programming skills.

3. Swapping Two Numbers without using the third variable


[package com.javaprogramto.numbers.swap;
public class SwapNumbersWIthoutThirdVariable {
public static void main(String[] args) {
int firstNumber = 10;
int secondNumber = 20;
System.out.println("Before swapping two numbers : firstNumber "
+ firstNumber + ", secondNumber " + secondNumber);
firstNumber = firstNumber + secondNumber;
secondNumber = firstNumber - secondNumber;
firstNumber = firstNumber - secondNumber;
System.out.println("After swapping two numbers : firstNumber "
+ firstNumber + ", secondNumber " + secondNumber);
}
}]


Output:

[Before swapping two numbers : firstNumber 10, secondNumber 20
After swapping two numbers : firstNumber 20, secondNumber 10]

The above program is tricky and does not need any third or temporary variable because it uses one of the available variables as the third variable. Almost look same but understanding is little tricky.

Seems like this also same as temp variable but as a result, it produces the same output.

First, summing firstNumber and secondNumber and storing the result in firstNumber. firstNumber  + secondNumber [10 + 20 = 30] After the first step here are the values for two variables.

[firstNumber = 30
secondNumber=20]

Next, Subtract two numbers as firstNumber - secondNumber (30 - 20 = 10) and store the result in secondNumber.

[firstNumber = 30
secondNumber=10]

Finally, again subtract two numbers as firstNumber - secondNumber [30 - 10 = 20] and store the result in firstNumber.

[firstNumber = 20
secondNumber=10]
This one of the logics without using the temp variable.

Another way:


But, produces the same results.

[ first = first - second;
 second = first + second;
 first = second - first;]

4. Conclusion

In conclusion, you've seen how to swap two numbers in java. Example java programs to with temp variable and without temp variable.

As usual, All the examples are shown available over GitHub.

Share to see the GitHub Code.

[lock]

[View on GitHub ##eye##]

[Download ##file-download##]

[/lock]

Swap Two Numbers in Java

using temp variable and without using third variable

0 Comments