swap the number without third varibale using pointer


Swap the Number without third variable by using pointer we can solve in different types but i am using a pointer with function in easy steps. Firstly enter two number then exchange value with swap() just firstly i added both variable into x just these type look below.


Description

x=10,  y=20

x=x+y    //Now see here x value updated now with 30

y=x-y     //Now y value is 30-20 is 10

x=x-y     //Now x value is 30-10 is 20


Now these same calcualtion with pointer i done below.
Now complete program start here swap the number without third varibale using pointer


#include < stdio.h >

#include < conio.h >

void swap ( int *x,int *y ) {

*x = (* x) + ( * y ) ;

*y = (* x) - ( * y ) ;

*x = (* x) - ( * y) ;

}

void main ( ) {

int  x , y ;

clrscr ( ) ;

printf ( “Enter two Number!” ) ;

scanf ( “%d% d ”, &x , &y ) ;

swap ( &x , &y ) ;

printf ( ‘The value of x after swap is %d  \n”,x ) ;

printf ( ‘The value of y after swap is %d  \n”,y ) ;

}


Output:
Enter two Number! 10 20
The value of x after swap is 20
The value of y after swap is 10