ProgrammingHardGATE CS · Competitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int der(int n) {
    if (n == 0) return 1;
    if (n == 1) return 0;
    return (n - 1) * (der(n - 1) + der(n - 2));
}
int main(void) {
    printf("%d", der(4));
}
  1. A. 8
  2. B. 9
  3. C. 6
  4. D. 24

Correct answer: B. 9

der(n) counts permutations with no fixed point, and der(4) = 3 * (der(3) + der(2)) = 3 * (2 + 1) = 9. The value 24 is 4!, which counts all permutations.

More C Programming questions

Exams that ask this topic