[C] factorial.c -- It computes repeatedly the factoria →→→→→进入此内容的聊天室

来自 Edgy Madrill, 2022-06-20, 写在 C, 查看 76 次.
URL http://www.code666.cn/view/166de9c2
  1. /* factorial.c -- It computes repeatedly the factorial of an integer entered
  2.  *        by the user. It terminates when the integer entered is not
  3.  *        positive.
  4.  */
  5.  
  6. #include <stdio.h>
  7.  
  8. int fact(int n);
  9.  
  10. int main(void) {
  11.   int current;
  12.  
  13.   printf("Enter a positive integer [to terminate enter non-positive] > ");
  14.   scanf("%d", &current);
  15.   while (current > 0) {
  16.     printf("The factorial of %d is %d\n", current, fact(current));
  17.     printf("Enter a positive integer [to terminate enter non-positive] > ");
  18.     scanf("%d", &current);
  19.   }
  20. }
  21.  
  22. /* n is a positive integer. The function returns its factorial */
  23. int fact(int n) {
  24.   int lcv;    /* loop control variable */
  25.   int p;      /* set to the product of the first lcv positive integers */
  26.  
  27.   for(p=1, lcv=2; lcv <= n; p=p*lcv, lcv++);
  28.   return p;
  29. }

回复 "factorial.c -- It computes repeatedly the factoria"

这儿你可以回复上面这条便签

captcha