C Example: GCD and LCM: Euclid's algorithm, iterative and recursive

🗓 Last updated: 2026-11-30👤 Written by: Gray✓ Reviewed by: MOTO🏷 Category: Algorithms

Provides both iterative and recursive Euclid's algorithm for GCD, then computes LCM as a/gcd*b with long long and boundary checks to avoid multiplication overflow. Tests 48/18, 17/5, 0/9, a negative pair and 1071/462.

🌱 Beginner Reference entries: kw-intkw-longfn-printf
🖥 Run OnlineGCC 10.2 · C11 standard · click ▶ to run · Copy code
🖨 Reference OutputMatches the online run in a standard C11 environment
C · GCC 10.2 · -std=c11
a=  48 b=  18  gcd_iter=  6  gcd_rec=  6  lcm=144
a=  17 b=   5  gcd_iter=  1  gcd_rec=  1  lcm=85
a=   0 b=   9  gcd_iter=  9  gcd_rec=  9  lcm=0
a= -24 b=  36  gcd_iter= 12  gcd_rec= 12  lcm=72
a=1071 b= 462  gcd_iter= 21  gcd_rec= 21  lcm=23562
🧩 Keep Learning

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏