Swap values of two integers a = 5 and b = 10 using arithmetic and print the result.
Ready — edit the code above and click Run.
using System;
class Program
{
static void Main()
{
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
Console.WriteLine($"a={a}, b={b}");
}
}
Try solving on your own first, then reveal the official answer.
Add/subtract trick: after a=a+b, subtracting b from new a yields original a.