Question discussion

What is the output of the following C program?#include void f(int n) { if (n > 0) { f(n - 1); printf("%d", n); f(n - 1); } } int main() { f…

1782Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C program?

#include <stdio.h>
void f(int n) {
    if (n > 0) {
        f(n - 1);
        printf("%d", n);
        f(n - 1);
    }
}
int main() {
    f(3);
    return 0;
}
  • A1232132
  • B3212123
  • C1231231
  • D1213121
Correct answer: D

Explanation

f(1) prints 1, f(2) prints 1 2 1, and f(3) prints (121) 3 (121), i.e. 1213121.

Discussion 0

Nobody has replied yet. Start the discussion!