C# Program to Swap Two Numbers
In C#, you can swap two numbers using a temporary variable like this:
int temp = a; a = b; b = temp; which exchanges the values of a and b.Examples
Inputa = 5, b = 10
Outputa = 10, b = 5
Inputa = -3, b = 7
Outputa = 7, b = -3
Inputa = 0, b = 0
Outputa = 0, b = 0
How to Think About It
To swap two numbers, think of holding one number temporarily so you don't lose it when you overwrite its place. Use a temporary container to store one number, then replace it with the other, and finally put the stored number into the second variable.
Algorithm
1
Get the two numbers to swap, call them a and b.2
Store the value of a in a temporary variable temp.3
Assign the value of b to a.4
Assign the value stored in temp to b.5
Now a and b have swapped values.Code
csharp
using System; class Program { static void Main() { int a = 5, b = 10; Console.WriteLine($"Before swap: a = {a}, b = {b}"); int temp = a; a = b; b = temp; Console.WriteLine($"After swap: a = {a}, b = {b}"); } }
Output
Before swap: a = 5, b = 10
After swap: a = 10, b = 5
Dry Run
Let's trace swapping a=5 and b=10 through the code
1
Initial values
a = 5, b = 10
2
Store a in temp
temp = 5
3
Assign b to a
a = 10
4
Assign temp to b
b = 5
| Variable | Value |
|---|---|
| a | 5 |
| b | 10 |
| temp | 5 |
| a | 10 |
| b | 5 |
Why This Works
Step 1: Use a temporary variable
We use temp to hold the value of a so it is not lost when we overwrite a.
Step 2: Assign b to a
We replace a with b's value, so now a has the second number.
Step 3: Assign temp to b
Finally, we put the original a value stored in temp into b, completing the swap.
Alternative Approaches
Using arithmetic operations
csharp
using System; class Program { static void Main() { int a = 5, b = 10; Console.WriteLine($"Before swap: a = {a}, b = {b}"); a = a + b; b = a - b; a = a - b; Console.WriteLine($"After swap: a = {a}, b = {b}"); } }
This method swaps without extra memory but can cause overflow with large numbers.
Using tuple deconstruction (C# 7.0+)
csharp
using System; class Program { static void Main() { int a = 5, b = 10; Console.WriteLine($"Before swap: a = {a}, b = {b}"); (a, b) = (b, a); Console.WriteLine($"After swap: a = {a}, b = {b}"); } }
This is the cleanest and most modern way but requires C# 7.0 or later.
Complexity: O(1) time, O(1) space
Time Complexity
Swapping two numbers uses a fixed number of operations, so it runs in constant time O(1).
Space Complexity
Only a few variables are used, so space complexity is constant O(1).
Which Approach is Fastest?
All methods run in O(1) time; tuple deconstruction is cleanest, arithmetic uses no extra variable but risks overflow.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Temporary variable | O(1) | O(1) | Simple and safe for all numbers |
| Arithmetic operations | O(1) | O(1) | No extra variable but risky with large numbers |
| Tuple deconstruction | O(1) | O(1) | Cleanest syntax, requires C# 7.0+ |
Use tuple deconstruction in modern C# for a clean and simple swap.
Forgetting to use a temporary variable or overwriting a value before saving it causes incorrect swaps.