ProgrammingHardWipro · Cognizant pattern

What is printed?

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

Correct answer: B. 10

f(4)=4+f(3)=4+3+f(2)=4+3+2+f(1)=4+3+2+1+f(0)=4+3+2+1+0=10.

More C Programming questions

Exams that ask this topic