ProgrammingHardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
unsigned long long g(unsigned n, unsigned long long acc) {
    return n == 0 ? acc : g(n - 1, acc * 3 + n);
}
int main(void) {
    printf("%llu", g(4, 1));
}
  1. A. 217
  2. B. 223
  3. C. 205
  4. D. 256

Correct answer: B. 223

The accumulator goes 1, 7, 24, 74, 223 as n counts down from 4 to 1, and the base case returns 223. The recursion is tail-recursive, so an iterative loop gives the same answer.

More C Programming questions

Exams that ask this topic