Write a recursive function to find the greatest common divisor of two integers n

Complete recursive function Fun (int n, int m) of the relevant code, the recursive function is used to calculate two positive integers greatest common divisor.
The greatest common factor, i.e., the greatest common divisor (in English: Greatest Common Divisor, abbreviated as the GCD; or Highest Common Factor, abbreviated as HCF) refers to a certain integer number of about a maximum one. The greatest common divisor of 12 and 18 is six.

int Fun(int n, int m)
{
    int a;
    if (m > n)
    {
        a = m % n;
        if (a == 0)
        {
            return n;
        }
        else
        {
            return Fun(a,n);
        }
    }
    else
    {
        a = n % m;
        if (a == 0)
        {
            return m;
        }
        else
        {
            return Fun(a,m);
        }
     }
}

or

int Fun(int n, int m)
{
l=(m>n)?n:m;
for(i=l; i>0; i--)  /*按照从大到小的顺序寻找满足条件的自然数*/
if(m%i==0 && n%i==0)
	{/*输出满足条件的自然数并结束循环*/
            return i;
            break;
        }
}
Published 102 original articles · won praise 93 · views 4958

Guess you like

Origin blog.csdn.net/huangziguang/article/details/104784960