ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
int f(int n) {
    if (n <= 0) return 0;
    return n % 2 ? f(n - 1) + n : f(n / 2) * 2;
}
int main(void) {
    printf("%d %d", f(10), f(7));
}
  1. A. 20 17
  2. B. 18 15
  3. C. 18 17
  4. D. 10 13

Correct answer: C. 18 17

Trace: f(1) = f(0) + 1 = 1, f(2) = f(1)·2 = 2, f(4) = f(2)·2 = 4, f(5) = f(4) + 5 = 9, f(10) = f(5)·2 = 18. For 7: f(3) = f(2) + 3 = 5, f(6) = f(3)·2 = 10, f(7) = f(6) + 7 = 17.

More C Programming questions

Exams that ask this topic