Saturday, December 4, 2010

Swap two variables without using third variable

Following is example to solve this problem.

suppose you have following two variable

a=6;
b=7;

Solution :

Function Swap(ref int x,ref int y)
{
x=x+y; //x=6+7 , x=13
y=x-y; //y=13-7, y=6
x=x-y; //x=13-6, x=7
}

No comments: