ProgrammingVery hardProduct company OA
What is printed?
What is printed?
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 main(void) { f(6); }- A. 6 5 4 3 2 1
- B. 6 3 2 1
- C. 6 3 1
- D. 6 2 1
Correct answer: B. 6 3 2 1
f(6): even, prints 6, calls f(3). f(3): odd, prints 3, calls f(2). f(2): even, prints 2, calls f(1). f(1): odd, prints 1, calls f(0), which returns without printing. Output: 6 3 2 1.
More C Programming questions
- 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)…
- What is printed? (binary string of n using recursion, division by 2) c void toBinary(int n) { if (n == 0) return; toBinary(n / 2)…
- What is printed? (two mutually exclusive recursive branches selected by a condition, with accumulation via return value) c int f(int n) {…