How to swap two numbers without using third variable in java

Three Simple ways to swap two numbers without using third variable


This article explains how to swap two numbers without using third temp variable. This java example of swapping two numbers without using third variable is important because it covers fundamentals and test your logic as well. This is the reason this is very important interview question for freshers. This is very important question for ICSE students as well.

We will see three methods to swap two numbers without using third temp variable.

1. Swap two numbers using addition and subtraction



In the example below first we are doing addition of two numbers and then subtracting the numberTwo from total. Then again subtracting new numberTwo from the total.


public class SwapNumberTest {

public static void main(String[] args) {

int numberOne = 36;

int numberTwo = 63;

System.out.println("Before Swapping numberOne = "+ numberOne +" numberTwo "+numberTwo );

numberOne = numberOne + numberTwo;

numberTwo = numberOne - numberTwo;

numberOne = numberOne - numberTwo;

System.out.println("After Swapping numberOne = "+ numberOne +" numberTwo "+numberTwo );

}

}


Output :
Before Swapping numberOne = 36 numberTwo 63
After Swapping numberOne = 63 numberTwo 36

1. Swap two numbers using multiplication and division technique


Second method is to use multiplication and division technique to swap two number without using third variable. In this method similar to addition and subtraction we are do multiply and divide operation as shown in the example below which is very self explanatory.


public class SwapNumberTest {

public static void main(String[] args) {

int numberOne = 3; //0011

int numberTwo = 6; //0110

System.out.println("Before Swapping numberOne = "+ numberOne +" numberTwo "+numberTwo );

numberOne = numberOne * numberTwo;

numberTwo = numberOne / numberTwo;

numberOne = numberOne / numberTwo;

System.out.println("After Swapping numberOne = "+ numberOne +" numberTwo "+numberTwo );

}

}


Output :
Before Swapping numberOne = 3 numberTwo 6
After Swapping numberOne = 6 numberTwo 3



1. Swap two numbers using XOR operation



Third method to swap the two numbers without using third variable is to use XOR operation. The binary XOR operation will always produce a 1 output if either of its inputs is 1 and will produce a 0 output if both of its inputs are 0 or 1. So as shown in the example below if we use XOR operation then we can easily swap the numbers


public class SwapNumberTest {

public static void main(String[] args) {

int numberOne = 3; //0011

int numberTwo = 6; //0110

System.out.println("Before Swapping numberOne = "+ numberOne +" numberTwo "+numberTwo );

numberOne = numberOne ^ numberTwo;

numberTwo = numberOne ^ numberTwo;

numberOne = numberOne ^ numberTwo;

System.out.println("After Swapping numberOne = "+ numberOne +" numberTwo "+numberTwo );

}

}


Output :
Before Swapping numberOne = 3 numberTwo 6
After Swapping numberOne = 6 numberTwo 3

Thus we have seen three ways to swap two numbers in java without using third or temp variable.
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment