assume two variable a=10 and b=20..
procedure:
a=a+b=10+20=30
b=a-b=30-20=10
a=a-b=30-10=20
so a=20
b=10 numbers r swapped...
2007-05-13 21:27:57
·
answer #1
·
answered by cool_navin1 3
·
0⤊
0⤋
Let there be two variables ' a ' and ' b ' then the values of a and b can be swapped without third variable as,
a, b
a = a + b;
b = a - b;
a = a - b;
hence the values are swapped;
example:
let, a=100, b=200;
a=a+b;
(a=100+200)
(a=300)
b=a-b;
(b=300-200)
(b=100)
a=a-b;
(a=300, b=100)
(a=300 -100)
(a=200)
hence values are swapped:
a=200 and b=100
2007-05-13 21:58:13
·
answer #2
·
answered by S a g a r 1
·
0⤊
0⤋
i think that's theoretically impossible. the computing device can no longer do 2 issues on the appropriate comparable time (computers use what's observed as "Time Sharing" to offer an phantasm of multi-tasking). For a working laptop or computing device to change variables, it would could exchange one variable at a time. After a metamorphosis has been made to the 1st variable, the 2nd variable won't be able to be properly replaced: assign: a = 4 b = 2 regulate first variable (a = b): a = 2 b = 2 regulate 2nd variable (b = a): a = 2 b = 2 a nil.33 variable could be utilized with a view to hold a non everlasting fee of what 'a' initally grow to be earlier it grow to be made to equivalent 'b'
2016-10-15 22:25:12
·
answer #3
·
answered by ? 4
·
0⤊
0⤋
many gave the following as the answer..
a = a + b;
b = a - b;
a = a - b;
.
.
.but there is a catch..
what if a+b results in overflow..
in general in c/c++ there is no default overflow handler..but if there is one then the given logic may fail miserably depending on the handler..
.
.
in java overflow exception will be thrown and if you don't have a proper catch block your program would crash miserably..
even if you have a catch block the statements following 'a+b' would not be executed and the values won't be swapped/....
.
.
.
.
if you really want integers top swap...
then its better to use the following...
.
.
a=a^b;
b=a^b;
a=a^b;
(i hope you know '^' is used for bitwise XOR operation).
.
.
now overflow is not possible..
.
.
.
but if your language don't care for the overflows then the first code would work as fine as the XOR based method..
.
.
.
but the second method would be executed pretty faster.
.
.
(in APLs also the first method would work fine..)
2007-05-14 02:38:16
·
answer #4
·
answered by thekillerkat 3
·
0⤊
0⤋
a = a + b;
b = a - b;
a = a - b;
a =1 b = 3
a = 1 + 3 = 4 ; a = 4 b = 3
b = 4 - 3 = 1 ; a = 4 b = 1
a = 4 - 1 = 3
a = 3 b = 1
a = a * b/a
b = a * b/a
2007-05-13 22:00:54
·
answer #5
·
answered by sagarukin 4
·
0⤊
0⤋
3 ways to do this .This is under the assumption that each instruction is sequential and autonomic as in most programming languages. Using + operator, Using - operator and using the *,/ operations on the variables.@ have already been listed by my super friends, 3 rd wont be hard to arrive at either on similar lines.
2007-05-13 21:55:35
·
answer #6
·
answered by Satya 3
·
0⤊
0⤋
int a,b;
a=5;
b=3;
a=a+b;
b=a-b;
a=a-b;
now the values are swapped with out third variable
result
a=3
b=5
2007-05-16 00:19:38
·
answer #7
·
answered by raji_badari 2
·
0⤊
0⤋
a = a + b;
b = a - b;
a = a - b;
2007-05-13 21:28:18
·
answer #8
·
answered by Aman J Singh 3
·
0⤊
0⤋