Friday 25 November 2011

Swap or Interchange two varibles without using a third variable

I am revealing Swaping two variables without using a third variable.
For readability I am using ANSI C format for coding ,You can use it in any language.

a=5;
b=10;
a=a+b;
b=a-b;
a=a-b;
Now a=10 and b=5.


But in traditional programing logic it will be
a=5
b=10
temp=a;
a=b;
b=temp;
Here using a third variable.It is not a good programming concept.










No comments:

Post a Comment

Search This Blog