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); }
  1. A. 6 5 4 3 2 1
  2. B. 6 3 2 1
  3. C. 6 3 1
  4. 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

Exams that ask this topic