How to solve greatest common devisor using recursion?

Question:

Need to solve the problem to find Greatest Common Devisor or HCF of two integers using recursion.

I did solve the solution but using while loop.

Asked By: Abhishek Yadav

||

Answers:

For recursion based this will work,
May be there are more edge cases, then I need to correct the solution.

def gcd(low, high):
  if low<=0:
    return high
  return gcd(high%low, low)

int HCF(int a, int b)
{
while (a != b)
{
if (a > b)
{
return HCF(a - b, b);
}
else
{
return HCF(a, b - a);
}
}
return a;
}

Answered By: Shreyash Pandey
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.