Friday, 17 February 2012

Swap two variable values using C++ programming language without using third variable or temporary variable



/*Without using any third varible*/

#include<iostream.h>

 Using namespace std

    void main()
      {
             int a,b ;
             a=1 ;
             b=2 ;
    
          SwapInC++(a , b) ;  //swap function call
     
          Cout<<"values after swap a= "<< a <<endl ;
    
          Cout<<"values after swap b= "<< b <<endl ;

       }  //End of main

    void SwapInC++(int x,int y)  //definition of Swap functon
    {
          y= x-y+(x=y);
     }

Common Programming Error:-
C++ compiler read from left to right to do not Confound with swap logic.

Good Programming Practice:-
always use equation like here i used instead using another temporary storage.

No comments:

Post a Comment

Compiler for C,C++ and Python {paste your programme to see the output}