ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}
int main(void) {
    printf("%d", gcd(48, 18));
}
  1. A. 12
  2. B. 18
  3. C. 48
  4. D. 6

Correct answer: D. 6

This is the Euclidean algorithm: gcd(48,18) -> gcd(18, 48%18=12) -> gcd(12, 18%12=6) -> gcd(6, 12%6=0) -> b==0, return a=6. The greatest common divisor of 48 and 18 is indeed 6.

More C Programming questions

Exams that ask this topic