For anyone looking for quick working code, try this:
I wrote a function lcm_n(args, num)
which computes and returns the lcm of all the numbers in the array args
. The second parameternum
is the count of numbers in the array.
Put all those numbers in an array args
and then call the function like lcm_n(args,num);
This function returns the lcm of all those numbers.
Here is the implementation of the function lcm_n(args, num)
:
int lcm_n(int args[], int num) //lcm of more than 2 numbers
{
int i, temp[num-1];
if(num==2)
{
return lcm(args[0], args[1]);
}
else
{
for(i=0;i<num-1;i++)
{
temp[i] = args[i];
}
temp[num-2] = lcm(args[num-2], args[num-1]);
return lcm_n(temp,num-1);
}
}
This function needs below two functions to work. So, just add them along with it.
int lcm(int a, int b) //lcm of 2 numbers
{
return (a*b)/gcd(a,b);
}
int gcd(int a, int b) //gcd of 2 numbers
{
int numerator, denominator, remainder;
//Euclid's algorithm for computing GCD of two numbers
if(a > b)
{
numerator = a;
denominator = b;
}
else
{
numerator = b;
denominator = a;
}
remainder = numerator % denominator;
while(remainder != 0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
return denominator;
}