Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to swap two numbers without using a temp variable in C#
Swapping two numbers without using a temporary variable can be achieved using different techniques in C#. These methods eliminate the need for extra memory allocation and demonstrate clever mathematical and bitwise operations.
Using Arithmetic Operations
The most common approach uses addition and subtraction operations to swap values −
val1 = val1 + val2; val2 = val1 - val2; val1 = val1 - val2;
Example
using System;
class Program {
static void Main(string[] args) {
int val1, val2;
val1 = 5;
val2 = 10;
Console.WriteLine("Values before swap...");
Console.WriteLine("val1: " + val1);
Console.WriteLine("val2: " + val2);
val1 = val1 + val2;
val2 = val1 - val2;
val1 = val1 - val2;
Console.WriteLine("Values after swap...");
Console.WriteLine("val1: " + val1);
Console.WriteLine("val2: " + val2);
}
}
The output of the above code is −
Values before swap... val1: 5 val2: 10 Values after swap... val1: 10 val2: 5
Using XOR Bitwise Operation
The XOR operation provides another efficient way to swap numbers without temporary variables −
val1 = val1 ^ val2; val2 = val1 ^ val2; val1 = val1 ^ val2;
Example
using System;
class Program {
static void Main(string[] args) {
int num1 = 15;
int num2 = 25;
Console.WriteLine("Before XOR swap:");
Console.WriteLine("num1: " + num1);
Console.WriteLine("num2: " + num2);
num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;
Console.WriteLine("After XOR swap:");
Console.WriteLine("num1: " + num1);
Console.WriteLine("num2: " + num2);
}
}
The output of the above code is −
Before XOR swap: num1: 15 num2: 25 After XOR swap: num1: 25 num2: 15
Using Multiplication and Division
For non-zero numbers, multiplication and division can also be used for swapping −
Example
using System;
class Program {
static void Main(string[] args) {
int x = 8;
int y = 12;
Console.WriteLine("Before multiplication swap:");
Console.WriteLine("x: " + x);
Console.WriteLine("y: " + y);
x = x * y;
y = x / y;
x = x / y;
Console.WriteLine("After multiplication swap:");
Console.WriteLine("x: " + x);
Console.WriteLine("y: " + y);
}
}
The output of the above code is −
Before multiplication swap: x: 8 y: 12 After multiplication swap: x: 12 y: 8
Comparison of Methods
| Method | Advantages | Disadvantages |
|---|---|---|
| Addition/Subtraction | Simple, works with all integers | Risk of overflow with large numbers |
| XOR Operation | No overflow risk, works with any integer | Less intuitive for beginners |
| Multiplication/Division | Alternative approach | Cannot be used with zero values |
Conclusion
Swapping numbers without temporary variables demonstrates efficient memory usage and mathematical operations in C#. The XOR method is generally preferred for its safety from overflow, while arithmetic methods are more intuitive for beginners to understand.
