C# Program to swap two numbers without third variable

How to write a C# Program to swap two numbers without using third variable.

C# Program to swap two number

using System;
 
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter first number");
            int n1 = int.Parse(Console.ReadLine());
            int n2 = int.Parse(Console.ReadLine());
            Console.WriteLine("Numbers Before swap n1 = " + n1 + " n2 = " + n2);
            n1 = n1 * n2; //n1=50 (5*10)      
            n2 = n1 / n2; //n2=5 (50/10)      
            n1 = n1 / n2; //n1=10 (50/5)    
            Console.Write("After swap n1 = " + n1 + " n2 = " + n2);
            Console.ReadLine();
        }
        
    }
}
 

 

Leave a Reply

Your email address will not be published.

five × 3 =