• Source
    1. // Given n of 1 or more, return the factorial of n, which is n * (n-1) * (n-2) ... 1. Compute the result // recursively (without loops).
    2.  
    3. // factorial(1) → 1
    4. // factorial(2) → 2
    5. // factorial(3) → 6
    6.  
    7. #include <stdio.h>
    8.  
    9. // To enable debug messages uncomment #define
    10. #define TEST 1
    11.  
    12. int factorial (int n);
    13. void startTesting();
    14.  
    15. int main(void) {
    16.  
    17. #ifdef TEST
    18. startTesting();
    19. #endif
    20.  
    21. return 0;
    22. }
    23.  
    24. int factorial(int n)
    25. {
    26. if (n == 0) {
    27. return 0;
    28. }
    29. if (n == 1) {
    30. return 1;
    31. }
    32.  
    33. return n * factorial (n -1);
    34. }
    35.  
    36. void startTesting()
    37. {
    38. int result = 0;
    39. int i = 0;
    40.  
    41. for (i = 0; i <= 5; i++) {
    42. result = factorial(i);
    43.  
    44. printf("Factorial(%d) = %d\n", i, result);
    45. }
    46. }