fork(1) 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. long Fact(int n)
  17. {
  18. long f;
  19. if(n==1||n==0) f=1;
  20. else f=n*Fact(n-1);
  21. return f;
  22. }
Success #stdin #stdout 0s 544KB
stdin
5
stdout
Input n:5! = 120