gcd and lcd in c
GCD (Greatest Common Divisor) in C:
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
int result = gcd(num1, num2);
printf("GCD of %d and %d is %d\n", num1, num2, result);
return 0;
}
LCM (Least Common Multiple) in C:
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
int result = lcm(num1, num2);
printf("LCM of %d and %d is %d\n", num1, num2, result);
return 0;
}
NOTE: These programs calculate the GCD and LCM of two numbers using the Euclidean algorithm. The GCD is the largest positive integer that divides both numbers without leaving a remainder, while the LCM is the smallest positive integer that is divisible by both numbers.