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));
}- A. 5000
- B. 5050
- C. 5151
- 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
- How many odd multiples of 3 are there in 1 to 30? Count them with this program. c #include <stdio.h> int main(void) { int c = 0; for…
- What is printed? c #include <stdio.h> int main(void) { int m[2][2] = {{3, 1}, {4, 2}}; int s = 0; for (int i = 0; i < 2; i++) { int lo =…
- What is printed? c #include <stdio.h> int main(void) { int r0[2] = {1, 2}, r1[2] = {3, 4}; int rows[2] = {r0, r1}; int t = rows[0]…
- What is printed? The reverse of the binary digits of 13 (1101) is taken as an integer. c #include <stdio.h> int main(void) { unsigned x =…
- How many occurrences of the two-letter string "ab" are in "abcabab"? Count them with this program. c #include <stdio.h> int main(void) {…
- What is printed? c #include <stdio.h> int main(void) { int c = 0; switch ('b') { case 'a': c++; case 'b': c += 10; case 'c': c += 100…