In Java, to swap values without using a third variable, we should understand operator concepts and how to assign values. In this article, we will learn how to swap two values without using a third variable through an example, detailed logic, and program explanation for better understanding.
For example, if the input numbers are a=23 and b=42, after swapping without using a third variable, the result will be a=42 and b=23.
Required Knowledge
- Java programming basics
- Java Operators
Problem Statement
We need to write a Java program to swap values without using a third variable. The program should initialize two variables with values and should swap values in those two variables without using a third variable. This action will be done in our program.
Let’s see an example,
Example:
For input integer values a =30, b=46, Output should be a=46 and b=30
Explanation:
Input integer number a =30
Input integer number b=46
Add a and b and Store it in a: a = a+b = 30+46 = 76
Get b value: b =a-b = 76-46 =30
Get a value: a = a-b = 76-30 = 46
Hence, for input numbers a = 30 and b = 46, we get a = 46 and b = 30 as output after swapping.
Logic to swap values without using third variable
- Take two values from the user.
- a = a+b
- Adds the values of a and b and assigns the result back to a. After this line, a now holds the sum of the original values of a and b.
- b= a-b
- Subtracts the original value of b from the new value of a and assigns the result to b. After this line, b now holds the original value of a.
- a= a-b
- Subtracts the new value of b (which is the original value of a) from the current value of a and assigns the result back to a. After this line, a now holds the original value of b.
- Now, the program will print the swapped result.
Java program to swap values without using third variable
In the below program, we are swapping two values without using a third variable in Java.
import java.util.Scanner;
public class SwappingValues {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter integer number a :");
int a = s.nextInt();
System.out.println("Enter integer number b :");
int b = s.nextInt();
System.out.println("Before Swapping: ");
System.out.println("a: "+a);
System.out.println("b: "+b);
a =a+b; //60+40=100
b=a-b;// b= 100-40=60
a=a-b;// a= 100-60= 40
System.out.println("After Swapping");
System.out.println("a: "+a);
System.out.println("b: "+b);
s.close();
}
}
Output:
Enter integer number a :
2024
Enter integer number b :
2000
Before Swapping:
a: 2024
b: 2000
After Swapping
a: 2000
b: 2024
Java program explanation to swap values without using third variable
- The above java program starts by asking the user to input two numbers.
- It reads the input values using a Scanner.
- Before swapping, it prints the original values of the two numbers.
- It performs the swapping without using a temporary variable using the following steps:
- Adds the two numbers and assigns the result to the first variable (
a = a + b
). - Subtracts the second number from the sum and assigns the result to the second variable (
b = a - b
). - Subtracts the original second number from the sum and assigns the result to the first variable (
a = a - b
).
- Adds the two numbers and assigns the result to the first variable (
- After swapping, it prints the new values of the two numbers.
- It closes the Scanner to release system resources.
Conclusion
In this article, we have learnt how to swap to two variables without using a third variable. Here, we understood how to use operators and assigning them properly for swapping purpose. We have seen the logic and program. Hope this article is helped you to understanding the program.
You can also check our another awesome tutorials
- Java program to swap two variables using a third variable
- Concept of operators in Java
- Program to find sum of all odd numbers in a range in Java
- Java program to calculate power without using pow method
Happy Learning!