A program in C to SWAP the contents of 3 variables without using the temporary variables.
/* Swapping 3 numbers without using extra variable */
#include< stdio.h >
#include< conio.h >
void Swap(int *a,int *b,int *c)
{
*a = *a + *b + *c;
*b = *a - (*b + *c);
*c = *a - (*b + *c);
*a = *a - (*b + *c);
}
int main()
{
int x=1,y=2,z=3;
clrscr();
printf("BEFORE SWAPPING : %d %d %d\n",x,y,z);
Swap(&x,&y,&z);
printf("AFTER SWAPPING : %d %d %d",x,y,z);
return(0);
}/*end*/
Tweet
Comments
Post a Comment