-3

Write a program to swap two variables without using a third variable as an intermediary position.

a=2; b=3;
a=a*b; // 6
b=a/b; // 2
a=a/b; // 3
a=3 and b=2

????is it true,,,i have errors in it

Baum mit Augen
  • 47,658
  • 24
  • 139
  • 177

2 Answers2

1

better use reversible operators without "singularities". Such as +, - (assuming integer wraparounds are ok.

And, of course, the winner is XOR.

a ^= b;
b ^= a;
a ^= b;
valdo
  • 12,216
  • 1
  • 35
  • 62
0

What happens if b or a is zero? If neither is zero and you do not get overflow of the type your code will work.

Boris Strandjev
  • 45,192
  • 14
  • 103
  • 128