Swap two numbers without using 3rd variable program in java


Swap two numbers without using 3rd variable 
program in java. 


  import java.util.*;
   class Swap {    
         public static void main(String args[])  
             {      
                int x, y;      
                System.out.println("Enter x and y");      
                Scanner in = new Scanner(System.in);  
                x = in.nextInt();      
                y = in.nextInt();         
          System.out.println("Before Swapping\nx = "+x+"\ny = "+y); 
          x = x + y;
          y = x - y;    
          x = x - y;        
          System.out.println("After Swapping\nx = "+x+"\ny = "+y);    
         } 



Home


Comments

Popular posts from this blog

A programe to convert binary into decimal using Function.

Function