ProgrammingHardWipro · Cognizant pattern

What is printed?

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

Correct answer: C. 6

This is the Euclidean GCD algorithm: mystery(48,18) -> mystery(18, 48%18=12) -> mystery(12, 18%12=6) -> mystery(6, 12%6=0) -> returns 6. GCD(48,18) = 6.

More C Programming questions

Exams that ask this topic