ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
int f(int n) {
if (n == 0) return 0;
return n + f(n - 1);
}
int main(void) {
printf("%d", f(4));
}- A. 24
- B. 10
- C. 4
- D. 14
Correct answer: B. 10
f(4)=4+f(3)=4+3+f(2)=4+3+2+f(1)=4+3+2+1+f(0)=4+3+2+1+0=10.
More C Programming questions
- What is printed? c int fib(int n) { if (n <= 1) return n; return fib(n - 1) + fib(n - 2); } int main(void) { printf("%d", fib(6)); }
- What is printed? c int f(int n) { if (n == 0) return 0; printf("%d ", n); if (n % 2 == 0) return f(n / 2); return f(n - 1); } int…
- How many TOTAL calls to fib (including the initial call) are made evaluating fib(4), given this naive recursive implementation? c int…
- What is printed? (recursion where the recursive call's result is deliberately ignored) c int calls = 0; int f(int n) { calls++; if (n <=…
- What is printed? (recursion depth vs. the array being modified on the way back up) c int a[5] = {1, 2, 3, 4, 5}; void f(int i) { if (i >=…
- What is printed? (recursion with an early return skipping the rest of the function) c int f(int n) { if (n < 0) return -1; if (n == 0)…