ft_recursive_factorial.c 741 B

123456789101112131415161718192021222324252627282930313233343536
  1. /* Create a recursive function that returns the factorial of the number given as a
  2. parameter.
  3. If there’s an error, the function should return 0
  4. */
  5. #include <stdio.h>
  6. int ft_recursive_factorial(int nb)
  7. {
  8. int factorial;
  9. if (nb < 0 || nb > 12) // на случай переполнения
  10. {
  11. return (0);
  12. }
  13. else if (nb == 0)
  14. {
  15. return (1);
  16. }
  17. else if (nb >= 1)
  18. {
  19. factorial = nb * ft_recursive_factorial(nb - 1);
  20. }
  21. return (factorial);
  22. }
  23. int main(void)
  24. {
  25. int a, b, c, d, e;
  26. a = ft_recursive_factorial(-1);
  27. b = ft_recursive_factorial(0);
  28. c = ft_recursive_factorial(1);
  29. d = ft_recursive_factorial(3);
  30. e = ft_recursive_factorial(10);
  31. printf("%d, %d, %d, %d, %d", a, b, c, d, e);
  32. }