Tuesday, June 11, 2019

Math - Find GCD

Find GCD


// Recursive function to return gcd of a and b
int gcd(int a, int b)
{   
    // Everything divides 0 
    if (a == 0 || b == 0)
       return 0;
    
    // base case
    if (a == b)
        return a;
    
    // a is greater
    if (a > b) 
        return gcd(a-b, b);
    return gcd(a, b-a);
}

No comments: