Swap a two number without using third variable:

Write a program to Swap a two number without using third variable.

Solution:

using System;
using System.Collections.Generic;

namespace Swapping
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Enter a number:");
            var num1 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter a number:");
            var num2 = Convert.ToInt32(Console.ReadLine());

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

            Console.WriteLine($"After swapping two number {num1}, {num2}.");
            Console.ReadLine();
        }

    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *