Page 1 of 212> Show 40 post(s) from this thread on one page

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   programme for swapping of two numbers without using temporary varaible (http://www.go4expert.com/forums/programme-swapping-using-temporary-t23944/)

 cindrilla 21Nov2010 14:59

programme for swapping of two numbers without using temporary varaible

hi frinds lets learn a programme to swap two numbers withouit using temporary variable .
this is in "c"
Code:

```/*swapping of two numbers*/ #include<stdio.h> main() { int a,b; printf("enter the values of a & b"); scanf("%d %d",&a,&b); printf("the values before swapping are %d,%d",a,b); a=a+b; b=a-b; a=a-b; printf("now the values after swapping are %d,%d",a,b); }```

 crazyNut 21Nov2010 20:59

Re: programme for swapping of two numbers without using temporary varaible

Quote:
 Originally Posted by cindrilla (Post 75015) hi frinds lets learn a programme to swap two numbers withouit using temporary variable . this is in "c" Code: ```/*swapping of two numbers*/ #include main() { int a,b; printf("enter the values of a & b"); scanf("%d %d",&a,&b); printf("the values before swapping are %d,%d",a,b); a=a+b; b=a-b; a=a-b; printf("now the values after swapping are %d,%d",a,b); }```

You can get a lot ways of doing that on google.

One of them is
a = a + b;
b = a - b;
a = a - b;

 seomanju 21Nov2010 22:00

Re: programme for swapping of two numbers without using temporary varaible

Thanks for providing this code.

 kemnet 22Nov2010 10:32

Re: programme for swapping of two numbers without using temporary varaible

lol was helpful. but im having trouble trying to imagine a situation where i wont be able to use a temp var. and why do u prefer scans an print to cout<< and cin

 virxen 22Nov2010 12:06

Re: programme for swapping of two numbers without using temporary varaible

Quote:
 Originally Posted by kemnet (Post 75069) lol was helpful. but im having trouble trying to imagine a situation where i wont be able to use a temp var. and why do u prefer scans an print to cout<< and cin
it's a tricky question in interviews for a job as a programmer.

 cindrilla 23Nov2010 19:42

Re: programme for swapping of two numbers without using temporary varaible

hey wht r u saying kemnet i dint get u

 kemnet 23Nov2010 19:44

Re: programme for swapping of two numbers without using temporary varaible

no nothing. i was just trying to imagine a situation where id love this code since i wont be able to use a Temporary Variable. but that got cleared up thanks

 cindrilla 23Nov2010 19:47

Re: programme for swapping of two numbers without using temporary varaible

Quote:
 Originally Posted by crazyNut (Post 75032) Have you googled it You can get a lot ways of doing that on google. One of them is a = a + b; b = a - b; a = a - b;
hey u !
i hadnt googled it i have just learnt this trick ya i agree tht there are several methods to do it i have just mentioned one of them okk

 cindrilla 23Nov2010 20:16

Re: programme for swapping of two numbers without using temporary varaible

hi frinds this can also be done in differnt methods
i would lyk to provide an alternate method instead of previous one as

a=a*b;
b=a/b;
c=a/b;
okk