-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
  • 49,044
  • 25
  • 144
  • 182

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,632
  • 2
  • 37
  • 67
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
  • 46,145
  • 15
  • 108
  • 135