fork download
  1. #include <stdio.h>
  2.  
  3.  
  4. int factorial_r(int n) {
  5. if (n <= 1) {
  6. return 1;
  7. } else {
  8. return n * factorial_r(n - 1);
  9. }
  10. }
  11.  
  12.  
  13. int factorial_l(int n) {
  14. int p = 1;
  15. for (int i = 1; i <= n; i++) {
  16. p = p * i;
  17. }
  18. return p;
  19. }
  20.  
  21. int main() {
  22. int recursive, loop;
  23. int num = 5;
  24.  
  25. recursive = factorial_r(num);
  26. loop = factorial_l(num);
  27.  
  28. printf("재귀호출: %d\n", recursive);
  29. printf("반복문: %d\n", loop);
  30.  
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
재귀호출: 120
반복문: 120