C# find the greatest common divisor

后端 未结 9 1523
夕颜
夕颜 2020-12-02 23:11

\"The greatest common divisor of two integers is the largest integer that evenly divides each of the two numbers. Write method Gcd that returns the greatest common divisor o

9条回答
  •  既然无缘
    2020-12-02 23:36

    Try this:

    public static int GCD(int p, int q)
    {
        if(q == 0)
        {
             return p;
        }
    
        int r = p % q;
    
        return GCD(q, r);
    }
    

提交回复
热议问题