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 ______.
Answer: A. GCD of x and y — 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…
- A.
GCD of x and y
- B.
yˣ
- C.
x × y
- D.
LCM of x and y
Attempted by 689 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: Iocl Engineers Officers Grade A Paper 2