SOLVED : Swap two variable

SOLVED:-How to swap 2 variable values without using a temp(3rd) variable.

6 comments:

KuldeepSingh said...

Let us assume two numbers and b with some initial values say a=5,b=6

Now perform the given below logic on original values of a and b

step 1: a=a-b;
step 2: b=a+b;
step 3: a=b-a;

after processing the logic, values of and b are swapped without use of any temporary variable.

Now a=6 and b=5

sathish.kulal said...

same solution u can write in below formate

b=a-b+(a=b)

sathish.kulal said...

Yes kuldeep is correct , but
adding/subtracting may cause overflow

KuldeepSingh said...

Sathish
Ur sol'n is very compact...Gud job

Unknown said...

Hi frendz, here is an another solutio for the problem :)

step 1: a=a*b;
step 2: b=a/b;
step 3: a=a/b;

Unknown said...

One more solution for the problem

step 1: a=a^b;
step 2: b=a^b;
step 3: a=a^b;