Google

Thursday, January 24, 2008

swaping of two numbers

In C languguage how can we swap two numbers

there are so many ways

here i interested to display some of the techniques

1) for ex. a=10,b=20

by using temporary variable : int a=10,b=10,t;
t=a;
a=b;
b=t;
2) without using temporary variable: int a=10,b=20;
a=a+b;
b=a-b;
a=a-b;

we can write the same thing using *, / combination,but it is problem when a or b value is 0.

3) swapping of 2 numbers in single statement :

b = (a+b)-(a=b);

No comments: