C# find the greatest common divisor

后端 未结 9 1518
夕颜
夕颜 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:45

    By using this, you can pass multiple values as well in the form of array:-
    
    
    // pass all the values in array and call findGCD function
        int findGCD(int arr[], int n) 
        { 
            int gcd = arr[0]; 
            for (int i = 1; i < n; i++) {
                gcd = getGcd(arr[i], gcd); 
    }
    
            return gcd; 
        } 
    
    // check for gcd
    int getGcd(int x, int y) 
        { 
            if (x == 0) 
                return y; 
            return gcd(y % x, x); 
        } 
    

提交回复
热议问题