public static int sum(int n) { return helper(n, 0); }
private static int helper(int n, int acc) {
if (n == 0) return acc;
return helper(n - 1, acc + n);
}
System.out.println(sum(4));What is printed?
- Acheck_circle
10
- B
4
- C
0
- D
24
Explanation
Tail-recursive accumulator sums 4+3+2+1 = 10.