Swap two Numbers without using a temporary variable in LinqPad

There are many ways swap two numbers. Now I am going to describe in this article that how to swap two numbers without using third variable.

Feeling so happy to share my first contribution.

First Method :

  1. int x = 10;  
  2. int y = 5;  
  3. // Code to swap 'x' and 'y'  
  4. x = x + y; // x now becomes 15  
  5. y = x - y; // y becomes 10  
  6. x = x - y; // x becomes 5  
  7. x.Dump();  
  8. y.Dump(); 

// In Linq Pad "Dump" Is used to print the statements like Console.WriteLine in C#

Output:

5
10

Second Method:

  1. int a = 10;  
  2. int b = 5;  
  3. // Code to swap 'x' and 'y'  
  4. a = a * b; // x now becomes 50  
  5. b = a / b; // y becomes 10  
  6. a = a / b; // x becomes 5  
  7. a.Dump();  
  8. b.Dump(); 

// In Linq Pad "Dump" Is used to print the statements like Console.WriteLine in C#

Output:

5
10