Here I am trying to find lowest common multiple of an array of numbers. I used the following formula to find the value which uses greatest common divisor to find out LCM.
My program calculates GCD correctly, but when it comes to find out LCM using GCD it gives wrong LCM value. What might be wrong in my logic. Any help would be much appreciated.
#include <stdio.h>
int main() {
int arr[10] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
int GCD = findGCD(arr[0], arr[1]);
int LCM = (arr[0] * arr[1]) / GCD;
int i;
for (i = 2; i < sizeof(arr) / sizeof(arr[0]); i++) {
int temp = GCD;
GCD = findGCD(temp, arr[i]);
LCM = (temp * arr[i]) / GCD;
}
printf("GCD IS %d AND LCM IS %d", GCD, LCM);
}
int findGCD(int num1, int num2) {
if (num2 == 0) {
return num1;
}
if (num1 % num2 == 0) {
return num2;
}
return findGCD(num2, num1 % num2);
}