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 :
- int x = 10;
- int y = 5;
-
- x = x + y;
- y = x - y;
- x = x - y;
- x.Dump();
- y.Dump();
// In Linq Pad "Dump" Is used to print the statements like Console.WriteLine in C#
Output:
5
10
Second Method:
- int a = 10;
- int b = 5;
-
- a = a * b;
- b = a / b;
- a = a / b;
- a.Dump();
- b.Dump();
// In Linq Pad "Dump" Is used to print the statements like Console.WriteLine in C#
Output:
5
10