View Poll Results: was this helpful?
good 1 33.33%
helpful 0 0%
veryhelpful 2 66.67%
not so 0 0%
Multiple Choice Poll. Voters: 3. You may not vote on this poll

programme for swapping of two numbers without using temporary varaible

Go4Expert Member
21Nov2010,14:59   #1
cindrilla's Avatar
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);
}

Last edited by shabbir; 21Nov2010 at 15:04.. Reason: Code blocks
Light Poster
21Nov2010,20:59   #2
crazyNut's Avatar
Quote:
Originally Posted by cindrilla View Post
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);
}

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;
Banned
21Nov2010,22:00   #3
seomanju's Avatar
Thanks for providing this code.
Go4Expert Member
22Nov2010,10:32   #4
kemnet's Avatar
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
Pro contributor
22Nov2010,12:06   #5
virxen's Avatar
Quote:
Originally Posted by kemnet View Post
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.
Go4Expert Member
23Nov2010,19:42   #6
cindrilla's Avatar
hey wht r u saying kemnet i dint get u
Go4Expert Member
23Nov2010,19:44   #7
kemnet's Avatar
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
Go4Expert Member
23Nov2010,19:47   #8
cindrilla's Avatar
Quote:
Originally Posted by crazyNut View Post
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
Go4Expert Member
23Nov2010,20:16   #9
cindrilla's Avatar
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
hope this will be helpful
Go4Expert Member
23Nov2010,20:39   #10
kemnet's Avatar
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