ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
int f(int n, int d) {
    if (d > n) return 0;
    if (n % d == 0) return d + f(n, d + 1);
    return f(n, d + 1);
}
int main(void) { printf("%d", f(6, 1)); }
  1. A. 6
  2. B. 9
  3. C. 3
  4. D. 12

Correct answer: D. 12

This sums all divisors of 6 from 1 upward: 1 (6%1==0) + 2 (6%2==0) + 3 (6%3==0) + 6 (6%6==0) = 12 (4,5 are skipped since they don't divide 6).

More C Programming questions

Exams that ask this topic