Tuesday, 13 October 2015

C#: Swapping two numbers without temporary variable

using System;
class Program
{
    public static void Main()
    {
        int Num1 = 20;
        int Num2 = 33;

        Console.WriteLine("\nNumbers before swapping");
        Console.WriteLine("First Number is  : {0}", Num1);
        Console.WriteLine("Second Number is : {0}", Num2);

        Num1 = Num1 + Num2;
        Num2 = Num1 - Num2;
        Num1 = Num1 - Num2;

        Console.WriteLine("\nNumbers after swapping");
        Console.WriteLine("First Number is  : {0}",Num1);
        Console.WriteLine("Second Number is : {0}",Num2);
    }
}

No comments:

Post a Comment