1. Home
  2. Docs
  3. C Programming Guides
  4. Some Algorithms
  5. Euclid’s Algorithm

Euclid’s Algorithm

#inlcude <stdio.h>
int greatest_common_divisor(int u, int v)
{
   int t;
   while ( u > 0 )
   {
       if( u < v )
       {
           t = u;
           u = v;
           v = t;
       }
       u = u - v;
   }
   return v;
}

main()
{
    int x, y;
    while (scanf("%d %d", &x, &y) != EOF)
    {
        if(x > 0 && y > 0)
        {
             printf("%d %d = %d \n", x, y, greatest_common_divisor(x, y));
        }
    }
}

Was this article helpful to you? Yes No

How can we help?