/*factorial.c - several versions of a factorial program. Copyright (c) 1998 Matthew Belmonte.*/ int main() { int n; long factorial; printf("Compute the factorial of what number? "); scanf("%d", &n); factorial = 1L; while(n > 0) factorial *= n--; printf("The factorial is %ld\n", factorial); return 0; } /*the same, but counting up to n instead of down to 0*/ int main() { register int count; int n; long factorial; printf("Compute the factorial of what number? "); scanf("%d", &n); factorial = 1L; count = 1; while(count <= n) factorial *= count++; printf("%d! = %ld\n", n, factorial); return 0; } /*an equivalent loop using 'for' instead of 'while'*/ int main() { register int count; int n; long factorial; printf("Compute the factorial of what number? "); scanf("%d", &n); for(factorial = 1L, count = 1; count <= n; count++) factorial *= count; printf("%d! = %ld\n", n, factorial); return 0; }