Advertisement
volkovich_maksim

factorial (non-recursive)

Sep 9th, 2016
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. long int fact(int n)
  4. {
  5.    int i;
  6.    long int f = 1;
  7.    for(i = 2;i <= n;i += 1)
  8.    {
  9.       f *= i;
  10.    }
  11.    return f;
  12. }
  13.  
  14. int main(void)
  15. {  
  16.    int n;
  17.    int qualifier = 1;
  18.    while (qualifier == 1 || qualifier == 0)
  19.    {
  20.       qualifier = scanf("%d", &n);
  21.       if (qualifier == 1 && n >= 0)
  22.          {
  23.             printf("%ld\n", fact(n));
  24.          }
  25.       if (qualifier == 1 && n < 0)
  26.          {  
  27.             printf("wrong type\n");
  28.          }
  29.    }
  30.    return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement