0
1.7kviews
Write a recursive and non-recursive function to calculate the GCD of two numbers.

Mumbai University > COMPS > Sem 3 > Data Structures

Marks: 5 M

Year: Dec 2014

1 Answer
0
3views
  • Here is the recursive function to calculate the GCD of two numbers a and b:-

    intgcd(int a, int b){
    while(a != b)
            if(a > b) return gcd(a-b, b);
            else return gcd(a, b-a)
        return a;
    }
    
  • Here is the non-recursive version:-

    int gcd(int a, int b){
        while(a != b)
            if (a >  b) a -= b;
            else b -= a;
        return a;
    }
    
Please log in to add an answer.