when you try to use the following function for the first time it will give you correct answer which gives you the LCM of two numbers ( Least common multiple ). for the second call with new parameters the static variable wont be starting at 1 which is going to give me wrong answer. is there anyway to set it up to 1 before doing his recursive loop ?
int lcm(int a, int b)
{
static int common = 1;
if (common % a == 0 && common % b == 0)
return common;
common++;
lcm(a, b);
return common;
}
lcm
as a parameter. Even better, replace the recursion with a loop. – HolyBlackCat Dec 24 '17 at 19:12private
static
into a class andfriend
functions as needed. – HolyBlackCat Dec 24 '17 at 19:24