#include int gcd(int a, int b); int gcd_mod(int a, int b); int gcd_rec(int a, int b); int lcm(int a, int b); int main() { int m, n, a, b; // Getting m and n from STDIN printf("Write m and n: "); scanf("%d %d", &m, &n); // Calculate the GCD using the gcd function int result = gcd(m, n); // Print the result printf("The Greatest Common Divisor (GCD) of m and n is \t%d (lecture)\n", result); // Calculate the GCD using the gcd function int result2 = gcd_mod(m, n); // Print the result printf("The Greatest Common Divisor (GCD) of m and n is \t%d (modulo)\n", result2); // Calculate the GCD using the gcd function int result3 = gcd_rec(m, n); // Print the result printf("The Greatest Common Divisor (GCD) of m and n is \t%d (recursion)\n", result3); // Calculate the LCM using the gcd function // e.g. lcm(4,6)=12 int result4 = lcm(m, n); // Print the result printf("The Least Common Multiple (LCM) of m and n is \t%d\n", result4); return 0; } int gcd(int a, int b) { /* GCD(a, b) - Lecture notes (substraction version) */ while (a != b) { if (a < b) { b = b - a; } else { a = a - b; } } return a; } int gcd_mod(int a, int b) { /* GCD(a, b) - Modulo version */ while (b != 0) { int t = b; b = a % b; a = t; } return a; } int gcd_rec(int a, int b) { /* GCD(a, b) - recursion version */ if (b == 0) { return a; } else { return gcd_rec(b, a % b); } } int lcm(int a, int b) { /* Calculate Least Common Multiple (LCM) using GCD LCM * GCD = m * n */ return (a * b) / gcd_mod(a, b); }