How to swap 2 integers without using a third variable?

int a = 3;
int b = 5;

Console.WriteLine(“Before Swap”);
Console.WriteLine(“a=” + a);
Console.WriteLine(“b=” + b);
a = a + b;
b = a – b;
a = a – b;

Console.WriteLine(“After Swap”);
Console.WriteLine(“a=” + a);
Console.WriteLine(“b=” + b);

 

Output

Before Swap
a=3
b=5
After Swap
a=5
b=3

Leave a Reply

%d bloggers like this: