Java program to swap two numbers with and without a third variable

In this post we explain two methods to swap two numbers in Java.
Method 1: swapping two numbers using a third variable
Method 2: swapping two numbers without a third variable.
1. Java program to swap two numbers using a third variable.
This is the simplest method to swap two numbers. We can swap two variables using a third or temporary (temp) variable by following the steps below:
Step 1: The value of the variable A is stored in the temp variable
temp = A;
Step 2: Once the value of A is stored, we can store the value of B in A
A = B;
Step 3: And finally the initial value of A (temp) is stored in B:
B = temp;
For example,
A = 1
B = 2
 
A
B
temp
 
1
2
-
temp = A
1
2
1
A = B
2
2
1
B = temp
2
1
1
It can also be done by first storing the value of B.
temp = B;
B = A;
A = temp;
Here is the complete Java code to swap two numbers using a temp variable:
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int A, B, temp;
        System.out.print("Input the first number: ");
        A = input.nextInt();
        System.out.print("Input the second number: ");
        B = input.nextInt();
        System.out.println("Before swapping: A = " + A + "   B = " + B);                                          
      
        temp = A;
        A = B;
        B = temp;
       
        System.out.println("After swapping: A = " + A + "   B = " + B);
    }
}
Sample output:
Input the first number: 1     
Input the second number: 2
Before swapping: A = 1   B = 2
After swapping: A = 2   B = 1

2. Java program to swap two numbers without third variable.
We can swap two numbers without a temp variable just by using arithmetic operators.
A and B are the variables to swap. The arithmetic operations to swap them are one addition followed by two subtractions.
A = A + B;
B = A – B;
A = A – B;
For example,
A = 2
B = 3
 
 
A
B
 
2
3
A = A + B
5
3
B = A - B
5
2
A = A - B
3
2
 
We can also swap them by performing one multiplication followed by two divisions:
A = A * B;
B = A / B;
A = A / B;
 
 
A
B
 
2
3
A = A * B
6
3
B = A / B
6
2
A = A / B
3
2
Here is the complete Java code to swap two numbres without third variable using the addition-subtraction method:
import java.util.Scanner;
 
public class JavaApplication360 {
 
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int A, B;
        System.out.print("Input the first number: ");
        A = input.nextInt();
        System.out.print("Input the second number: ");
        B = input.nextInt();
        System.out.println("Before swapping: A = " + A + "   B = " + B);                                          
 
        A = A + B;
        B = A - B;
        A = A - B;
 
        System.out.println("After swapping: A = " + A + "   B = " + B);
    }
}
Sample output:
Input the first number: 2     
Input the second number: 3
Before swapping: A = 2   B = 3
After swapping: A = 3   B = 2

5 comments :

  1. Have you checked this JAVA program to Swap two variables or number.One can find almost all the methods of swapping of two numbers.

    ReplyDelete
  2. Nice post.For more other ways to swap two number refer this link http://techno-terminal.blogspot.in/2015/08/java-program-to-swap-two-numbers.html

    ReplyDelete
  3. Nice, also check my post
    http://www.mycodingland.com/2015/11/java-program-to-exchange-values-of-two.html

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete