1、
#include <stdio.h> int fac(int x) { int i, fac = 1; if(x > 0) { for(i = x; i > 0; i--) fac *= i; } else fac = 1; return fac; } int main(void) { int a; printf("a = "); scanf("%d", &a); printf("result: %d\n", fac(a)); return 0; }
2、
#include <stdio.h> int fac(int x) { int i, fac = 1; if(x > 0) { for(i = 1; i <= x; i++) { fac *= i; } } else fac = 1; return fac; } int main(void) { int a; puts("please input an integer."); printf("a = "); scanf("%d", &a); printf("the result: %d\n", fac(a)); return 0; }