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

cindrilla's Avatar, Join Date: Sep 2010
Go4Expert Member
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
0
crazyNut's Avatar, Join Date: Nov 2010
Light Poster
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;
0
seomanju's Avatar
Banned
Thanks for providing this code.
0
kemnet's Avatar, Join Date: Nov 2010
Go4Expert Member
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
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
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.
0
cindrilla's Avatar, Join Date: Sep 2010
Go4Expert Member
hey wht r u saying kemnet i dint get u
0
kemnet's Avatar, Join Date: Nov 2010
Go4Expert Member
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
0
cindrilla's Avatar, Join Date: Sep 2010
Go4Expert Member
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
0
cindrilla's Avatar, Join Date: Sep 2010
Go4Expert Member
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
0
kemnet's Avatar, Join Date: Nov 2010
Go4Expert Member
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