write a program to interchange the values of 2 variables without using 3rd variable?
Raman
2010-09-04 06:56:55 UTC
write a program to interchange the values of 2 variables without using 3rd variable
Four answers:
Mark M
2010-09-04 07:02:05 UTC
You can do it by addition and subtraction of the existing values (shown as x and y here):
x = x + y
y = x – y
x = x – y
?
2010-09-04 08:52:05 UTC
let x =10 and y = 20
then for interchange
x=x+y //x=10+20=30
y=x-y //y=30-20=10
x values is interchanged
and
x=x-y //x=30-10=20
value of x and y is interchanged
?
2016-10-20 08:28:52 UTC
-> Swapping with third variable #contain #contain void substantial() { int a, b; clrscr(); printf("enter the two numbers:"); scanf("%dp.c.d",&a,&b); printf("Values in the past swapping are:%dp.c.d",a,b); int t; t=a; a=b; b=t; printf("The values after swapping are:%dp.c.d",a,b); getch(); } ->Swapping without the third variable #contain #contain void substantial() { int a, b; clrscr(): printf("enter the two numbers:"); scand("%dp.c.d",&a,&b); printf("Values in the past swapping are:%dp.c.d",a,b); a=a+b; b=a-b; a=a-b; printf("The values after swapping arep.c.dp.c.d",a,b); } desire this permits........:)
?
2010-09-04 07:00:18 UTC
This might help you: http://en.wikipedia.org/wiki/Swap_%28computer_science%29
Would've been good to know what language you're using.
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.