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:

  1. class swap   
  2. {  
  3.     public static void main(String arg[])   
  4.     {  
  5.         System.out.println("Before swapping");  
  6.         int a = 20;  
  7.         int b = 40;  
  8.         System.out.println("value of a:" + a);  
  9.         System.out.println("value of b:" + b);  
  10.         System.out.println("After swapping");  
  11.         a = a ^ b;  
  12.         b = a ^ b;  
  13.         a = a ^ b;  
  14.         System.out.println("value of a:" + a);  
  15.         System.out.println("value of b:" + b);  
  16.     }  
  17. }
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).



Step 4: open command prompt.



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



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



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



Happy codding.
Ebook Download
View all
Learn
View all