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;
// Code to swap 'x' and 'y'
x = x + y; // x now becomes 15
y = x - y; // y becomes 10
x = x - y; // x becomes 5
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;
// Code to swap 'x' and 'y'
a = a * b; // x now becomes 50
b = a / b; // y becomes 10
a = a / b; // x becomes 5
a.Dump();
b.Dump();// In Linq Pad "Dump" Is used to print the statements like Console.WriteLine in C#
Output:
5
10
