Here I will show you how you can swap two numbers A=20 and B=10 with and without temporary variables as A=10 and B=20.

## Swap Numbers with Temporary Variable

I will take one more temporary variable as temp to keep value of B so that during copy of value A to B value of B is in temp and now we can copy value of temp to A. As below

temp=B **(This state : A=20, B=10, temp=10)**

B=A; ** (This state : A=20, B=20, temp=10)**

A=temp **(This state : A=10, B=20, temp=10)**

By applying above logic you can swap values of two variables. In below Java example I will implement same logic two swap numbers.

## Swap Numbers without Temporary Variable

There are two ways two swap two numbers without using any temporary variable.

### Addition and Subtraction

Here you will see how to swap numbers A=20 and B=10 by using addition and subtraction operations.

A=A+B ** (This state : A=30, B=10)**

B=A-B ** (This state : A=30, B=20)**

A=A-B ** (This state : A=10, B=20)**

By applying above addition and subtraction logic you can swap values of two variables. In below Java example I will implement same logic two swap numbers.

### Multiplication and Division

Here you will see how to swap numbers A=20 and B=10 by using multiplication and division operations.

A=A*B **(This state : A=200, B=10)**

B=A/B **(This state : A=200, B=20)**

A=A/B **(This state : A=10, B=20)**

By applying above multiplication and division logic you can swap values of two variables. In below Java example I will implement same logic two swap numbers.

## Swap Numbers Java Program

package test; import java.util.Scanner; public class SwapNumbers { public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("Please Enter First Number"); int A=scan.nextInt(); System.out.println("Please Enter Second Number"); int B=scan.nextInt(); //swap Numbers with temporary variable swapNumbersWithTemp(A,B); //swap Numbers without temporary variable : Add and Sub swapNumbersWithoutTempAddSub(A,B); //swap Numbers without temporary variable : Mul and Div swapNumbersWithoutTempMulDiv(A,B); } private static void swapNumbersWithTemp(int A, int B) { int temp=B; B=A; A=temp; System.out.println("Numbers after swap with temporary variables"); System.out.println("A="+A+"and B="+B); } private static void swapNumbersWithoutTempAddSub(int A, int B) { A=A+B; B=A-B; A=A-B; System.out.println("Numbers after swap without temporary variables: Add and Sub operation"); System.out.println("A="+A+"and B="+B); } private static void swapNumbersWithoutTempMulDiv(int A, int B) { A=A*B; B=A/B; A=A/B; System.out.println("Numbers after swap without temporary variables: MUl and Div operation"); System.out.println("A="+A+"and B="+B); } }

## Output

Please Enter First Number 20 Please Enter Second Number 10 Numbers after swap with temporary variables A=10and B=20 Numbers after swap without temporary variables: Add and Sub operation A=10and B=20 Numbers after swap without temporary variables: MUl and Div operation A=10and B=20 <span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span><span

## More Info

For more Algorithms and Java Programing Test questions and sample code follow below links

Pingback: Frequently Asked Algorithms and Programs in Interviews | Facing Issues On IT