ProgrammingHardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
long long sum(long long n, long long acc) {
    return n == 0 ? acc : sum(n - 1, acc + n);
}
int main(void) {
    printf("%lld", sum(100, 0));
}
  1. A. 5000
  2. B. 5050
  3. C. 5151
  4. D. 4950

Correct answer: B. 5050

The accumulator adds 100, 99, ..., 1, which is the triangular number 100 * 101 / 2 = 5050.

More C Programming questions

Exams that ask this topic