by Shekh Ali
Swapping two numbers is a fundamental task in programming, and in this article, we will write a C# Program to Swap Two Numbers in various ways.
Table of Contents
- 0.1 Method 1: C# Program to Swap Two Numbers Using a Temporary Variable
- 0.2 Method 2: C# Program to swap two numbers without third variable
- 0.3 Method 3: Program to Swap Two Numbers Using XOR Bitwise Operation
- 1 Conclusion
- 1.1 Related
Method 1: C# Program to Swap Two Numbers Using a Temporary Variable
One of the simplest ways to swap two numbers is by using a temporary variable.
This method involves three steps:
Storing the value of the first variable in the temporary variable, assigning the value of the second variable to the first, and finally, assigning the value stored in the temporary variable to the second variable.
// C# program to swap two numbers using a temporary variableusing System;class Program{ static void Main() { int num1 = 10, num2 = 20, temp; // Displaying original values Console.WriteLine($"Original Values: num1 = {num1}, num2 = {num2}"); // Swapping using a temporary variable temp = num1; num1 = num2; num2 = temp; // Displaying swapped values Console.WriteLine($"Swapped Values: num1 = {num1}, num2 = {num2}"); }}
Output:
Original Values: num1 = 10, num2 = 20Swapped Values: num1 = 20, num2 = 10
Code Explanation:
In this method, we use a temporary variable (temp
) to store the value of num1
before assigning the value of num2
to it. This ensures that the original value of num1
is not lost during the swap.
Finally, we assign the temporary variable temp
value to the num2
variable to complete the swap.
Method 2: C# Program to swap two numbers without third variable
In the code example below, we will try to swap two numbers usingarithmetic operations without a temporary variable.
using System;class Program{ static void Main() { // Declare and initialize two numbers int num1 = 5, num2 = 10; Console.WriteLine($"Before swapping: num1 = {num1}, num2 = {num2}"); // Swap without using a temporary variable num1 = num1 + num2; // Now, num1 is 15 (5 + 10) num2 = num1 - num2; // Now, num2 is 5 (15 - 10) num1 = num1 - num2; // Now, num1 is 10 (15 - 5) Console.WriteLine($"After swapping: num1 = {num1}, num2 = {num2}"); }}
Output:
Before swapping: num1 = 5, num2 = 10After swapping: num1 = 10, num2 = 5
Code Explanation:
- Step 1: Add num1 and num2 and store the result in num1. Now, num1 is 15 (5 + 10)
- Step 2: Subtract num2 from the num1 and store the result in num2. Now, num2 is 5 (15 – 10)
- Step 3: Subtract the num2 from the updated num1 and store the result in num1. Now, num1 is 10 (15 – 5)
Method 3: Program to Swap Two Numbers Using XOR Bitwise Operation
XOR bitwise operation is an efficient way to swap two numbers without using a temporary variable. Below is the code example.
// C# program to swap two numbers using XOR bitwise operationusing System;class Program{ static void Main() { int num1 = 10, num2 = 20; // Displaying original values Console.WriteLine($"Original Values: num1 = {num1}, num2 = {num2}"); // Swapping using XOR bitwise operation num1 = num1 ^ num2; num2 = num1 ^ num2; num1 = num1 ^ num2; // Displaying swapped values Console.WriteLine($"Swapped Values: num1 = {num1}, num2 = {num2}"); }}
Output:
Original Values: num1 = 10, num2 = 20Swapped Values: num1 = 20, num2 = 10
Code Explanation:
- Initially,
num1
is 10, andnum2
is 20. - We print the values before swapping:
num1 = 10, num2 = 20
. - We then perform the swapping using XOR operations.
- Step 1:
num1
becomes the XOR of the original values ofnum1
andnum2
, so it becomes 30 (10 ^ 20). - Step 2:
num2
becomes the XOR of the updatednum1
and the originalnum2
, so it becomes 10 (30 ^ 20). - Step 3:
num1
becomes the XOR of the updatednum1
and the updatednum2
, so it becomes 20 (30 ^ 10). - We print the values after swapping:
num1 = 20, num2 = 10
.
Conclusion
In this article, we learned three different methods to write aC# Program to Swap Two Numbers.
Each method has its own merits, and the choice between them may depend on specific requirements or performance considerations. As a C# developer, these techniques will enhance your problem-solving skills and broaden your understanding of fundamental programming practice. Happy coding!
Recommended Articles:
- How to remove duplicate characters from a String in C#
- C# program to count the occurrences of each character in a String
- C# program to count vowels and consonants in a string
- Leap Year Program in C, C++, C#, JAVA, PYTHON, and PHP
- Program to print prime numbers from 1 to N
- C# Program to Check if a Given Number is Even or Odd
- Bubble sort programs in C, C++, JAVA, and PYTHON
- Fibonacci sequence: Fibonacci series in C# (with examples)
- Program to copy all elements of an array into another array
- Different Ways to Calculate Factorial in C# (with Full Code Examples)
- C# Programs asked in Interviews
- Author
- Recent Posts
Shekh Ali
Meet Sheikh Ali, a skilled Software engineer and passionate a blogger. He enjoys sharing his expertise in C#, Java, SQL, Design Patterns, and other related topics through his writing. When he's not coding, Sheikh Ali can often indulge in his passions for reading books and fitness training.
Latest posts by Shekh Ali (see all)
- C# Program to Capitalize the First Character of Each Word in a String - February 21, 2024
- C# Program to Find the Longest Word in a String - February 19, 2024
- How to Reverse an Array in C# with Examples - February 12, 2024