Tuesday, 13 October 2015

C#: Swapping two numbers using third (temporary) variable

using System;
class Program
{
    public static void Main()
    {
        int Num1, Num2, temp;

        Console.WriteLine("Enter First Number");
        Num1 = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter Second Number");
        Num2 = int.Parse(Console.ReadLine());

        temp = Num1;
        Num1 = Num2;
        Num2 = temp;

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

No comments:

Post a Comment