Consider the following recursive function. int function (int x, int y) { if (y…

2020

Consider the following recursive function.

int function (int x, int y) {
   if (y <= 0) return x;
   return function (y, x % y);
}

The above recursive function computes ______.

  1. A.

    GCD of x and y

  2. B.

  3. C.

    x × y

  4. D.

    LCM of x and y

Attempted by 408 students.

Show answer & explanation

Correct answer: A

The given code implements Euclid's algorithm. It repeatedly calls function(y, x % y) until y <= 0. When the remainder becomes 0, the current x is the greatest common divisor of the two numbers. Therefore, the function computes the GCD, also called HCF.

Correct option: A

Explore the full course: Coal India Management Trainee