Thursday, 6 November 2014

Write a program to swap two numbers without using temporary variable

2) Swap without using temporary variable

In the last post we have seen swap with temp variable. Eventually we have so many methods to do this and we have just tried to put all here. Contribution by comments are most welcome. Now let's look at the java program to swap two numbers without using any temporary variable. The below versions uses arithmetic based swapping of two numbers. One should have two variables to swap.



Method 1:
The below code uses Arithmetic Operators to swap two numbers. It uses two  operators they are addition and subtraction to swap two numbers. While adding two integers one should take care of the integer value may overflow. If both a and b are to large after the addition then it may go out of integer range.

 import java.util.*;  
 import java.lang.*;  
 class Swap2  
 {   
   public static void main(String args[])  
   {  
     int a=11,b=22;  
     System.out.printf("Values before swap are  a=%d and b=%d",a,b);  
     a=a+b;   
     b=a-b;  
     a=a-b;  
     System.out.printf("\nValues after swap are a=%d and b=%d",a,b);  
   }  
 }  

Method 2:
The below code uses Arithmetic Operators to swap two numbers. It uses two  operators say multiplication and division to swap two numbers. But one need to take care that the operands should not be 0(zero) otherwise this approach doesn't work.

 import java.util.*;  
 import java.lang.*;  
 class Swap2
 {   
   public static void main(String args[])  
   {  
     int a=11,b=22;  
     System.out.printf("Values before swap a=%d and b=%d",a,b);  
     a=a*b;  
     b=a/b;  
     a=a/b;  
     System.out.printf("\nValues after swap a=%d and b=%d",a,b);  
   }  
 }  

Method 3:
Bitwise XOR
 import java.util.*;  
 import java.lang.*;  
 class Swap3  
 {   
   public static void main(String args[])  
   {  
     int a=11,b=22;  
     System.out.printf("Values before swap a=%d and b=%d",a,b);  
     a^=b;  
     b^=a;  
     a^=b;  
     System.out.printf("\nValues after swap a=%d and b=%d",a,b);  
   }  
 }  

Method 4:

 import java.util.*;  
 import java.lang.*;  
 class Swap4  
 {   
   public static void main(String args[])  
   {  
    int a=5,b=6;   
    System.out.printf("Values before swap a=%d and b=%d",a,b);   
    b=a+b-(a=b);  
    System.out.printf("\nValues after swap a=%d and b=%d",a,b);   
   }  
 }  

Method 5: 

 import java.util.*;  
 import java.lang.*;  
 class Swap5  
 {   
   public static void main(String args[])  
   {  
    int a=5,b=6;   
    System.out.printf("Values before swap a=%d and b=%d",a,b);   
    a = (a*b)/(b=a);  
    System.out.printf("\nValues after swap a=%d and b=%d",a,b);   
   }  
 }  

Method 6:


  import java.util.*;   
  import java.lang.*;  
  class Swap6  
  {   
   public static void main(String args[])  
   {  
   int a=5,b=6;   
   System.out.printf("Values before swap a=%d and b=%d",a,b);   
   a=b-~a-1;  
   b=a+~b+1;  
   a=a+~b+1;  
   System.out.printf("\nValues after swap a=%d and b=%d",a,b);   
   }  
  }  

Method 7:

  import java.util.*;   
  import java.lang.*;  
  class Swap7  
  {   
   public static void main(String args[])  
   {  
   int a=5,b=6;   
   System.out.printf("Values before swap a=%d and b=%d",a,b);   
   a =((a = a + b) - (b = a - b));  
   System.out.printf("\nValues after swap a=%d and b=%d",a,b);   
   }  
  }  



3 comments:
Write comments
  1. I am curious to find out what blog system you happen to
    be working with? I'm having some small security issues with my latest website and I would like to find something more secure.
    Do you have any solutions?

    Take a look at my blog post cheap jordan shoes

    ReplyDelete
  2. He answers it himself: "we each carry inside our heart a divine element World of Warriors Hack lehman has
    made $3 billion from real estate sales since filing for
    bankruptcy, according to court filings.

    ReplyDelete

Featured post

List of Universities in Karnataka offering M.Sc Computer Science

The post-graduate programme in Computer Science (M.Sc Computer Science) contains two academic years duration and having a four semesters....

Popular Posts

Copyright @ 2011-2016