Swap 2 numbers Program in C sharp – using different techniques

Swap 2 numbers Program in C sharp – Learn how to interchange values between 2 variables, with and without using temporary variables ,functions with call be reference and using arithmetic and bitwise XOR operators. Also learn how to code the logic in single line to  interchange values between 2 variables.

Swapping means interchanging.For example if you have two variables Number1 and Number2 where Number1 =5 and Number2 = 6, then before intechanging the values Number1 =5 and Number2 = 6 and after interchanging the values Number1 =6 and Number2 = 5.

Swap 2 numbers Program in C sharp

C# Example program to swap 2 numbers by using third variable

In this program we will use a third variable as temporary variable to interchange 2 numbers.

  using System;
  class Program
    {
        static void Main(string[] args)
        {
            //initializing num1 by 10 and num2 by 20
            int num1 = 10, num2 = 20, temp ;

            Console.WriteLine("Numbers before interchanging : \n num1 = " + num1 + " \n num2 = " + num2);
            //logic to interchange values from num1 --> num2 and num2 --> num1
            //temp variabable is a intermediate variable for interchanging 2 numbers
            temp = num1; 
            num1 = num2;
            num2 = temp;

            Console.WriteLine("Numbers after interchanging : \n num1 = " + num1 + "  \n num2 = " + num2);
        }
    }
Output

Numbers before interchanging :
num1 = 10
num2 = 20
Numbers after interchanging :
num1 = 20
num2 = 10

 

C# Example program to swap 2 numbers without using third variable

Example program- using + and – operators

using System;
class Program
    {
        static void Main(string[] args)
        {
            //initializing num1 by 10 and num2 by 20
            int num1 = 10, num2 = 20;

            Console.WriteLine("Numbers before interchanging : \n num1 = " + num1 + " \n num2 = " + num2);

            //logic to interchange values from num1 --> num2 and num2 --> num1
           // interchanging the numbers using arithmetic operators like + and -
            num1 = num1 + num2;
            num2 = num1 - num2;
            num1 = num1 - num2;

            Console.WriteLine("Numbers after interchanging : \n num1 = " + num1 + "  \n num2 = " + num2);

        }
   
    }

Example program- using * and / operators

using System;
class Program
    {
        static void Main(string[] args)
        {
            //initializing num1 by 10 and num2 by 20
            int num1 = 10, num2 = 20;

            Console.WriteLine("Numbers before interchanging : \n num1 = " + num1 + " \n num2 = " + num2);

            //logic to interchange values from num1 --> num2 and num2 --> num1
           // interchanging the numbers using arithmetic operators like * and /
            num1 = num1 * num2;
            num2 = num1 / num2;
            num1 = num1 / num2;

            Console.WriteLine("Numbers after interchanging : \n num1 = " + num1 + "  \n num2 = " + num2);

        }   
    }

C# Example program  using bitwise XOR operator

using System;
class Program
    {
        static void Main(string[] args)
        {
            //initializing num1 by 10 and num2 by 20
            int num1 = 10, num2 = 20;

            Console.WriteLine("Numbers before interchanging : \n num1 = " + num1 + " \n num2 = " + num2);

            //logic to interchange values from num1 --> num2 and num2 --> num1

            num1 = num1 ^ num2;
            num2 = num1 ^ num2;
            num1 = num1 ^ num2;

            Console.WriteLine("Numbers after interchanging : \n num1 = " + num1 + "  \n num2 = " + num2);
        }
 }

C# Example program using function with ref keyword

class Program
    {
        static void Main(string[] args)
        {
            //initializing num1 by 10 and num2 by 20 
            int num1 = 10, num2 = 20;
            Console.WriteLine("Numbers before interchanging : \n num1 = " + num1 + " \n num2 = " + num2);
            Program p = new Program(); p.swap(ref num1, ref num2);
            Console.WriteLine("Numbers after interchanging : \n num1 = " + num1 + " \n num2 = " + num2);
        }
        //swap function definition 
        void swap(ref int a, ref int b)
        {
            int t; //logic to interchange values from num1 --> num2 and num2 --> num1 
            t = b;
            b = a;
            a = t;
        }
    }

C# Examples with in single line using XOR, (+,-),(*,/) operators

Example program using XOR operator

    using System;
    class Program
    {
        static void Main(string[] args)
        {
           //initializing num1 by 10 and num2 by 20
            int num1 = 10, num2 = 20;

            Console.WriteLine("Numbers before interchanging : \n num1 = " + num1 + " \n num2 = " + num2);
	
             // Swap using XOR operator
            num1 = num1 ^ num2 ^ (num2 = num1);
                       
            Console.WriteLine("Numbers after interchanging : \n num1 = " + num1 + "  \n num2 = " + num2);
        }
    }

Example program using (+,-) operator

    using System;
    class Program
    {
        static void Main(string[] args)
        {
           //initializing num1 by 10 and num2 by 20
            int num1 = 10, num2 = 20;

            Console.WriteLine("Numbers before interchanging : \n num1 = " + num1 + " \n num2 = " + num2);

            //Swap using +,- operator 
            num1 = num1 + num2 - (num2 = num1);   

            Console.WriteLine("Numbers after interchanging : \n num1 = " + num1 + "  \n num2 = " + num2);
        }
    }

Example program using (*,/)operator

    using System;
    class Program
    {
        static void Main(string[] args)
        {
           //initializing num1 by 10 and num2 by 20
            int num1 = 10, num2 = 20;

            Console.WriteLine("Numbers before interchanging : \n num1 = " + num1 + " \n num2 = " + num2);
            
	//Swap using *,/ operator.
             num1 = num1 * num2 /(num2 = num1);
           
            Console.WriteLine("Numbers after interchanging : \n num1 = " + num1 + "  \n num2 = " + num2);
        }
    }

Notes

Swapping used in sorting algorithms to arrange the data in ascending or descending order depends on criteria.

Related Posts