ft_iterative_factorial.c 780 B

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