Hello Everyone, in this tutorial we will learn about Java Program to Swap Two Numbers Without using Third Variable. Swapping in Java Programming Language means exchanging values of two variables. For example, suppose there are two variables, a and b; the value of a is 10 and b is 20; after swapping, the value of a will become 20, and the value of b will become 10.
In the previous tutorial, we learned how to swap two numbers using the third variable, but in this tutorial, we will swap the numbers without using the third variable. Instead of that, we will use the addition and subtraction method. The logic is given below.
- First of all, we will declare the variables.
- Now, we will assign the values to the variables.
- Next, we will assign the sum of the first and second variables to the first variable.
- Next, we will subtract the second variable from the first variable and store this value in the second variable.
- Next, we will subtract the second variable from the first variable and store this value in the first variable.
Algorithm
1 2 3 4 5 6 7 8 9 |
START DECLARE FIRST AND SECOND VARIABLE ASSIGN VALUES TO FIRST AND SECOND VARIABLE PRINT FIRST VARIABLE AND SECOND VARIABLE FIRST VARIABLE = FIRST VARIABLE + SECOND VARIABLE SECOND VARIABLE = FIRST VARIABLE - SECOND VARIABLE FIRST VARIABLE = FIRST VARIABLE - SECOND VARIABLE PRINT FIRST VARIABLE AND SECOND VARIABLE END |
Java Program to Swap Two Numbers Without using Third Variable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class Swapping { public static void main(String[] args){ int a,b; //Declaring all the variables a=10; //assigning value to variable a b=20; //assigning value to variable b //printing the value of a before swapping System.out.println("The value of a before swapping = "+a); //printing the value of b before swapping System.out.println("The value of b before swapping = "+b); //Swapping Logic a=a+b;//assigning the sum of the first and second variables to the first variable. b=a-b;//subtracting the second variable from the first variable and store this value in the second variable. a=a-b;//subtracting the second variable from the first variable and store this value in the first variable. //printing the value of a after swapping System.out.println("The value of a after swapping = "+a); //printing the value of b after swapping System.out.println("The value of b after swapping = "+b); } } |
1 2 3 4 |
The value of a before swapping = 10 The value of b before swapping = 20 The value of a after swapping = 20 The value of b after swapping = 10 |
What we Did?
- First of all, we have created a class and named it Swapping.
- After that, started main() method of the program.
- Then, inside the main() method, declared variables.
- Then, assigned values to the variables.
- Then, printed the value of the variables before swapping.Then implemented the swapping logic using the addition and subtraction method without using the third variable, which is as follows
- First of all, assigned the sum of the first and second variables to the first variable.
- Then, subtract the second variable from the first variable and stored this value in the second variable.
- Then, subtract the second variable from the first variable and stored this value in the first variable.
- Then finally, printed the value of the variables after swapping.
The Java program to Swap Two Numbers without Using Third Variable has been successfully completed. In the next tutorial, we will learn Java Program to add two complex numbers.