Google Interview Question
Country: India
First of all you need to clarify what you need to do if 0 is one of the arguments. I will just skip this case.
The gcd function below uses Euclidean algorithm. Do a search if you don't know it.
int gcd(int a, int b)
{
while(b)
{
int tmp = b;
b = a mod b;
a = tmp;
}
return tmp;
}
int lcm(int a, int b)
{
int gcd = gcd(a,b);
return a*b/gcd;
}
- Sam February 07, 2012