IdeaMonk

thoughts, ideas, code and other things...

Saturday, June 16, 2007

Swapping two vars without using third one

Now this is pretty easy, basically taught to anyone who begins programming BASIC, C, Java etc. More importantly it lets one ponder about how actually the 'swap' function of BASIC works. When you have figured out that, then you say, oh yea, so this is how swap works, maybe next day I can figure out how printf in C works, or what about atoi ?!
And what else ? Besides, all said above, it also teaches you how to optimize our work by eating less memory! so here's our little snippet -
#include <stdio.h>

int main (void) {
int a=5,b=7;
printf (" a = %i , b = %i \n",a,b);
a=a+b;
b=a-b;
a=a-b;

printf ("after swap \n a = %i , b = %i \n",a,b);

system("pause");
return 0;
}

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home