C# - Euclidean Algorithm for GCD

By , 10/13/2009
(1 ratings)
Implementation of the Euclidean Algorithm for finding the Greatest Common Denominator between 2 integers.
public int GCD(int a, int b)
{
     while (a != 0 && b != 0)
     {
         if (a > b)
            a %= b;
         else
            b %= a;
     }

     if (a == 0)
         return b;
     else
         return a;
}
Tagged with test, c#, math.

Comments

 

Log in, to comment!