Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. // external libraries
  2. #include <stdio.h>
  3.  
  4. // constants
  5. #define EXIT_CODE -1
  6. #define MESSAGE_FOR_ENTER_NUMBER "Type the factorial number (0 to exit program)"
  7. #define MESSAGE_FACTORIAL_RESULT "Result:"
  8.  
  9. // prototypes
  10. int factorial(int number);
  11.  
  12.  
  13. // main program
  14. // loop to enter a number while number is <> EXIT_CODE
  15. int main(void) {
  16.  
  17. int number;
  18. int result;
  19.  
  20. do {
  21. printf("%s\n", MESSAGE_FOR_ENTER_NUMBER);
  22.  
  23. scanf("%d", &number);
  24.  
  25. if (number >= 0) {
  26. result = factorial(number);
  27.  
  28. printf("%s %d\n\n", MESSAGE_FACTORIAL_RESULT, result);
  29. }
  30.  
  31. } while (number != EXIT_CODE);
  32.  
  33. return 0;
  34. }
  35.  
  36.  
  37. // recursive factorial function
  38. // return the factorial number from @number
  39. int factorial(int number) {
  40. if (number == 0)
  41. return 1;
  42.  
  43. return number * factorial(number - 1);
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement