swap two number without using any third variable and arithmetic operator.

In this article we learn how to swap two number without using any third variable and arithmetic operator.

Step 1: lets open the notepad and write the following code.

//code

 

class swap

{

                public static void main(String arg[])

                {

                                System.out.println(“Before swapping”);

                                int a=20;

                                int b=40;

                                System.out.println(“value of a:”+a);

                                System.out.println(“value of b:”+b);

                                System.out.println(“After swapping”);

                                a=a^b;

                                b=a^b;

                                a=a^b;

                                System.out.println(“value of a:”+a);

                                System.out.println(“value of b:”+b);

                               

                }

}

 

Step 2:  Name it as “sp.java” and save the file in any location. i saved at “C:/kiran/program”.

 step 3: open command prompt(press window +R and write cmd and hit ok).

1

Step 4: open command prompt

.2

 

step 5: Go to “C:/kiran/program” by using cmd.

3

Step 6: now, write the following code for cheking my java file is compile or not.

javac sp.java

4

my java file is compiled succesfully.

 

 

Step 7: write the following code in command prompt. press enter and see the output.

java demo

//demo is a class name which is written in my “hasrel.java” file.

 

output:

5

Happy codding.

Tags: ,