/* * greatest common divisor, least common multiple */ #include unsigned int gcd(unsigned int a, unsigned int b) { if (a == 0) return b; if (b == 0) return a; while(a != b) { if(a > b) a -= b; else b -= a; } return a; } unsigned int gcd_recursive(unsigned int a, unsigned int b){ if (b != 0) return gcd_recursive(b, a%b); else return a; } unsigned int lcm(unsigned int a, unsigned int b) { return a*b/gcd(a, b); } int main() { unsigned int n1 = 8, n2 = 12; printf("GCD of %d and %d is %d\n", n1, n2, gcd(n1, n2)); printf("GCD of %d and %d is %d (recursion)\n", n1, n2, gcd_recursive(n1, n2)); printf("LCM of %d and %d is %d\n", n1, n2, lcm(n1, n2)); return 0; } /* GCD of 8 and 12 is 4 GCD of 8 and 12 is 4 (recursion) LCM of 8 and 12 is 24 */