728x90 AdSpace

Latest News
Wednesday, June 5, 2013

Swapping without 3rd Variable

Swapping Without 3rd Variable..!!

We know that Swapping it is the process of exchanging the values between the variables.

Generally in order to swap we use a 3rd variable.

The following sudo code is what the general process of swapping..!!

For example if the value of A=10 and B= 20. To swap the values we use a temporary value C.

C=A

( The value of A will be moved to C. Now C =10 and A has no values)

A=B

( Since the value of A is empty, the value of B is moved to A )

B=C

( Now the value of A that is being stored in C is moved to B ) 

the above method will swap the values of A and B with the Help of 3rd variable. 

But we can swap without the 3rd variable.

The following step will do swapping without the 3rd variable

A = A + B

( A= 10+20 i.e A = 30 )

B = A - B

( B = 30-20 i.e B = 10 )

A = A- B

( A = 30-10 i.e A=20 )

Yes now the Value of A = 20 and B =10

Our target of swapping without 3rd variable is done..:) :)




Have a great day readers



Praveen Kumar Rajendran

Chennai
  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment

Item Reviewed: Swapping without 3rd Variable Rating: 5 Reviewed By: Praveen Kumar Rajendran