Swap two numbers with & without using the third variable
Tags: java, programming, codenewbie, computerscience
Published: 2022-03-16 09:51:14

**Swap two numbers with using the third variable**

Explanation :
_We will be define two variables and a temp variable. Initialize variable(a),(b) from user input using Scanner class. Here we will simply use 3 step logic to swap the two variables with the help of temp variable. STEP 01 assign variable(a) to variable(temp). STEP 02 assign variable(b) to variable(a). STEP 03 assign variable(temp) to variable(b). Finally print the swapped variables._

Code :
```
import java.util.Scanner;

public class SwapTwo {

	public static void main(String[] args) {
		/* initialize variables */
		int a, b, temp;

		/* Input from the user */
		System.out.println("Enter values:");
		Scanner sc = new Scanner(System.in);

		System.out.print("a = ");
		a = sc.nextInt();

		System.out.print("b = ");
		b = sc.nextInt();

		/*Assign value of variable a to temp*/
		temp = a;

		/*Assign value of variable b to a*/
		a = b;

		/*Assign value of variable temp to b*/
		b = temp;

		/* finally print the swapped values */
		System.out.println("After swap : a = " + a + ", b = " + b);
	}

}
```
Output :
`Enter values:
a = 45
b = 64
After swap : a = 64, b = 45`

Steps : 
1. Define two variables and a temp variable.
2. Initialize variable from the users inputs.
3. Assign value of variable(a) to variable(temp).
4. Assign value of variable(b) to variable(a).
5. Assign value of variable(temp) to variable(b).
6. Finally, print the swapped variables.


**Swap two numbers without using the third variable**

Explanation :
_We will be define two variables and declare them from user end using Scanner class. Using simple logic to swap the two variables with 3 steps. STEP 01 we will get the sum of two variables and assign it to 1st variable(a). STEP 02 we will be deduct the value of 2nd variable(b) from the sum(a=a+b) and assign it to the 2nd variable(b). STEP 03 we will repeat the STEP 02 but here the value of 2nd variable is changed so that the answer will be the initial value of 2nd variable. Finally print the swapped variables._

Code :
```
import java.util.Scanner;

public class SwapTwoNum {

	public static void main(String[] args) {
		/* initialize variables */
		int a, b;

		/* Input from the user */
		System.out.println("Enter values:");
		Scanner sc = new Scanner(System.in);

		System.out.print("a = ");
		a = sc.nextInt();

		System.out.print("b = ");
		b = sc.nextInt();

		/*get sum of both a and b, assign it to a*/
		a = a + b;

		/* Subtracting the value of b from the sum(a=a+b), assign it to b*/
		b = a - b;

		/* assign a by subtracting sum(a=a+b) by b where b value is swapped*/
		a = a - b;

		/* finally print the swapped values */
		System.out.println("After swap : a = " + a + ", b = " + b);
	}

}
```
Output :
`Enter values:
a = 34
b = 56
After swap : a = 56, b = 34`

Steps : 
1. Define two variables.
2. Initialize variables from the users inputs.
3. Get sum of both a and b(a+b) and assign it to variable "a".
4. Subtract the value of "b" from the sum(a=a+b), assign it to variable "b".
5. Assign "a" with the value of sum(a=a+b) - "b" the new swapped variable.
6. Finally, print the swapped variables.

**Special Note**: This may see simple but expressing the logic is the difficult part in explanation. Hope this will help someone in future!! I welcome your feedback and solutions-oriented suggestions, thank you for your time.

Happy Coding❤️❤️❤️