1. Home
  2. /
  3. Docs
  4. /
  5. C Programming Guides
  6. /
  7. Some Algorithms
  8. /
  9. 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));
        }
    }
}

Still stuck? Contact

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *

All Rights Reserved 2025.