C# Return Greatest Common Divisor (GCD)

The greatest common divisor (gcd), also known as the greatest common denominator, greatest common factor (gcf), or highest common factor (hcf), of two or more non-zero integers, is the largest positive integer that divides the numbers without a remainder.

For example, the GCD of 8 and 12 is 4. (wiki)

 

using System;

public class Program
{
    static int GCD(int num1, int num2)
    {
        int Remainder;

        while (num2 != 0)
        {
            Remainder = num1 % num2;
            num1 = num2;
            num2 = Remainder;
        }

        return num1;
    }

    static int Main(string[] args)
    {
        int x, y;

        Console.Write("Enter the First Number: ");
        x = int.Parse(Console.ReadLine());

        Console.Write("Enter the Second Number: ");
        y = int.Parse(Console.ReadLine());

        Console.Write("\nThe Greatest Common Divisor of ");

        Console.WriteLine("{0} and {1} is {2}", x, y, GCD(x, y));

        Console.ReadLine();
        return 0;
    }
}

 

Output