C# Program to Swap Two Numbers (With Multiple Examples)

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.

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 variable
using 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 = 20
Swapped 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 using arithmetic 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 = 10
After 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 operation
using 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 = 20
Swapped Values: num1 = 20, num2 = 10

Code Explanation:

  • Initially, num1 is 10, and num2 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 of num1 and num2, so it becomes 30 (10 ^ 20).
  • Step 2: num2 becomes the XOR of the updated num1 and the original num2, so it becomes 10 (30 ^ 20).
  • Step 3: num1 becomes the XOR of the updated num1 and the updated num2, 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 a C# 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:

Shekh Ali
4 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments