Calculates the greatest common divisor (GCD) of given two numbers. Use the Euclidean algorithm. - CSharp Data Structure Algorithm

CSharp examples for Data Structure Algorithm:Algorithm

Description

Calculates the greatest common divisor (GCD) of given two numbers. Use the Euclidean algorithm.

Demo Code

using System;/*from   w ww  . ja  va 2s . co  m*/


class GCD
{
    static void Main()
    {
        int num1 = int.Parse(Console.ReadLine());
        int num2 = int.Parse(Console.ReadLine());
        while (num1 != 0 && num2 != 0)
        {
            if (num1 > num2)
            {
                num1 -= num2;
            }
            else
            {
                num2 -= num1;                
            }
        }
        Console.WriteLine(Math.Max(num1, num2));
    }
}

Related Tutorials