How to swap two numbers without using a temporary variable?

 

Method 1

 

 

Drawback – Doesn’t work if one of number is ‘0’ 

Method 2

 

 

Drawback – Doesn’t work if one of number is ‘0’

Method 3

 

 

If you like this Article, then don’t forget to Click on Social likes buttons.

 

  • voorth

    Actually, method 1 does work for x or y being 0:

    (x, y) = (0, 5)
    x = x + y (5, 5)
    y = x - y (5, 0)
    x = x -y (5, 0)

    (x,y) =(5, 0)
    x = x + y : (5, 0)
    y = x - y : (5, 5)
    x = x - y : (0, 5)

  • thanx voorth
    i appreciate ur suggestion