--- In [email protected], Srikanth M R <[EMAIL PROTECTED]> wrote: > > Hello Blue Crystal... > > Your second method is absolutely fine. There is a > minor change to be made in the 1st method. > > --- Blue Crystal <[EMAIL PROTECTED]> wrote: > > > Hello Dude, here is some more method of swapping of > > two numbers. > > Works number that is not Zero. > > ie, A!=B!=0; > > > > a=a*b; > > a=a/b; > > b=b/a; > > You got the order wrong It is > > a=a*b; > b=a/b; > a=a/b; > > > 2nd method works for all values. > > a=b+a-(b=a); > > I donot know why the following method should work at > all. But I have not verified it. > > > I guess this method works for negative numbers. > > a=a^b; > > b=b^a; > > a=a^b;
I dare to disagree. As pointed out by Paul (through a link) all those methods have a couple of disadvantages. The most important is that there are no range checks. It will often happen that the multiply/divide method will not yield exact results. Or that the XOR method cannot work for anything but integer numbers; it cannot work reliably for float or double values (at least not without some additional effort). Or that the plus/minus method will overflow in the middle of the calculation, leading to strange errors which are hard to detect. Before posting such "solutions", make sure you have looked at all potential special cases or circumstances under which such a method may be used. Regards, Nico
