Java Program on Swapping Two Numbers Without Using Third Variable

Java Program on Swapping Two Numbers Without Using Third Variable

Swapping two numbers without using third or temporary variable is one of the most commonly asked question not just on Java but also on C and C++ tests and interviews. Swapping two numbers or strings is a very easy question till the condition of using only two variable is not applied, lots of freshers failed to solve this swap without using the third variable. Mostly we come out with a solution with just arithmetic operator but if the test level is little up and asks about other approaches of swapping two variables without using third or temp variable, than you have to look over this little program.
Since you are looking for a swap two numbers program in java without using any temp variable, let me tell that this post is a complete resource to learn different approaches on swapping two numbers numbers without using third variable.



Description:

We just have to swap(interchange) the 2 numbers, stored in 2 different variables. Don't forget we are not using the third variable.
example:
INPUT>   x=12 & y=53
OUTPUT> x=53 & y=12

As I told you earlier, you may be asked to swap two numbers with different approaches without using any temporary or third variable. This post contains four methods to do so!

Method 1: Swapping Using Arithmetic Operator
In 90% of cases this method strikes first to a fresher while swapping without using third variable.
import java.io.*;
class swap{
        public static void main(String args[]) throws IOException{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter 1st no: ");
            int x=Integer.parseInt(br.readLine());
            System.out.print("Enter 2nd no: ");
            int y=Integer.parseInt(br.readLine());
            System.out.println("\nNumbers Before Swapping:");
            System.out.println("x = "+x);
            System.out.println("y = "+y);
            //Swapping Method 1
            x=x+y;
            y=x-y;
            x=x-y;
            System.out.println("\nNumbers After Swapping:");
            System.out.println("x = "+x);
            System.out.println("y = "+y);
        }
}


Method 2: Swapping Using Bitwise Operators
Second approach two swap to numbers without using third variable is using XOR bitwise operators.
import java.io.*;
class swap{
        public static void main(String args[]) throws IOException{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter 1st no: ");
            int x=Integer.parseInt(br.readLine());
            System.out.print("Enter 2nd no: ");
            int y=Integer.parseInt(br.readLine());
            System.out.println("\nNumbers Before Swapping:");
            System.out.println("x = "+x);
            System.out.println("y = "+y);
            //Swapping Method 2
            x = x^y;
            y = x^y;
            x = x^y;
            System.out.println("\nNumbers After Swapping:");
            System.out.println("x = "+x);
            System.out.println("y = "+y);
        }
}


Method 3: Swapping Using Multiplication & Division
Like  Method 1 where we swapped two numbers x and y without using third variable by the simple logic of addition and subtraction, In this method we do the same swapping using multiplication and division logic keeping in mind that we don't have to use third variable.
import java.io.*;
class swap{
        public static void main(String args[]) throws IOException{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter 1st no: ");
            int x=Integer.parseInt(br.readLine());
            System.out.print("Enter 2nd no: ");
            int y=Integer.parseInt(br.readLine());
            System.out.println("\nNumbers Before Swapping:");
            System.out.println("x = "+x);
            System.out.println("y = "+y);
            //Swapping Method 3
            x = x*y;
            y = x/y;
            x = x/y;
            System.out.println("\nNumbers After Swapping:");
            System.out.println("x = "+x);
            System.out.println("y = "+y);
        }
}

Method 4: Swapping Using 'One Line Logic!'
Now here comes my favorite method to swap two numbers... Its the best method for swapping because only one line logic is required for this. i.e,  x = y-x+(y=x);
import java.io.*;
class swap{
        public static void main(String args[]) throws IOException{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter 1st no: ");
            int x=Integer.parseInt(br.readLine());
            System.out.print("Enter 2nd no: ");
            int y=Integer.parseInt(br.readLine());
            System.out.println("\nNumbers Before Swapping:");
            System.out.println("x = "+x);
            System.out.println("y = "+y);
            //Swapping Method 4
            x = y-x+(y=x);
            System.out.println("\nNumbers After Swapping:");
            System.out.println("x = "+x);
            System.out.println("y = "+y);
        }
}

OUTPUT:
Swapping two numbers in Java

So that's it guys now you know four ways to swap two variables without using third variable in Java. Its good to know multiple ways to swap two variables without using temporary or third variable.
Now its your time to show your response, If you liked this post and feels you learned something great than hit the like/+ button below, share this awesome articles with your friends and let they know these three approaches which we can apply while swapping two numbers without using third variable.


If I forgotten something or you know any other approach, do let us know. Having problem in swapping number in Java or any other program comment you question below!

7 Comments Leave new

import java.io.*;
public class InterChange
{
public static void main()throws IOException
{
int x, y, z;
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
System.out.println("Enter the Value of x");
x=Integer.parseInt(br.readLine());
System.out.println("Enter the Value of y");
y=Integer.parseInt(br.readLine());
System.out.println("Before InterChanging");
System.out.println("x value="+x);
System.out.println("y value="+y);
z=y;
y=x;
x=z;
System.out.println("After InterChanging");
System.out.println("x value="+x);
System.out.println("y value="+y);
}
}

Reply

Hello Maneesh...
Thanks for sharing your program for swapping using 3 variables :)

Reply

I knew only the mutliplication and divison one . Thanks for telling more.

Reply

Glad to know you liked our post! Keep visiting....

Reply
This comment has been removed by the author.

one line logic is outstanding sir ..thank u

Reply

Hi, I like your way of teaching. Thank you. Could you please explain this one line logic x = y-x+(y=x). Thanks in Advance.

Reply

Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.