ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
int f(int n) {
    if (n == 0) return 1;
    int a = f(n - 1);
    return a * n;
}
int main(void) {
    long long result = 1;
    for (int i = 1; i <= 3; i++) {
        result *= f(i);
    }
    printf("%lld", result);
}
  1. A. 6
  2. B. 9
  3. C. 24
  4. D. 12

Correct answer: D. 12

f computes n! (factorial): f(1)=1, f(2)=2, f(3)=6. The loop multiplies result by f(1)*f(2)*f(3) = 1*1*2*6 = 12 (result starts at 1, then *=1 stays 1, *=2 becomes 2, *=6 becomes 12).

More C Programming questions

Exams that ask this topic