fork download
  1. #include <stdio.h>
  2. long Fact(int n);
  3. int main(void)
  4. {
  5. int n;
  6. long result;
  7. printf("Input n:");
  8. scanf("%d", &n);
  9. result = Fact(n); /* 调用递归函数Fact()计算n! */
  10. if(result == -1) /* 处理非法数据 */
  11. printf("n<0, data error!\n");
  12. else /* 输出n!值 */
  13. printf("%d! = %ld\n", n, result);
  14. return 0;
  15. }
  16. /* 函数功能:用递归法计算n!,当n>=0时返回n!,否则返回-1 */
  17. long Fact(int n)
  18. {
  19. if (n<0)
  20. return -1;
  21. else if (n==0||n==1)
  22. return 1;
  23. else
  24. return(n*Fact(n-1));
  25.  
  26.  
  27. }
  28.  
Success #stdin #stdout 0s 5320KB
stdin
5
stdout
Input n:5! = 120