Wednesday, 23 November 2011

3 ways to do swaping of two numbers

1-With the use of third variable:

                                                 If  a = 5 and b = 6 then
                                                 let int c = 0;
                                                 c = a+b =>11;
                                                 a = c - a =>6;
                                                 b = c - b =>5;
           So now    a = 6 and b = 5

2-Without the use of third variable

                                                 If  a = 5 and b = 6 then
                                                 a = a+b =>11;
                                                 b = a- b =>5;
                                                 a = a - b =>6;
           So now    a = 6 and b = 5
3-Without the use of ( + , - ) operators:  

                                                 If  a = 5 and b = 6 then
                                                 a = a*b =>30;
                                                 b = a / b =>5;
                                                 a = a / b =>6;
           So now    a = 6 and b = 5
         

No comments:

Post a Comment