// for循环计算阶乘 #include <stdio.h> int get_input(void); int fac(int); main() { int input; int ans; printf("This progame calculates factorial.\n"); printf("Please Input a number :\n"); input = get_input(); ans = fac(input); if (ans=-1) printf("Calculate the factorial of %d ? Are you sure?\n", input); else printf("The factorial of %d is %d\n", input, ans); } int get_input(void) { int input; char ch; while (scanf_s("%d", &input) != 1) { while ((ch = getchar()) != ‘\n‘) putchar(ch); printf(" is Not a number\n"); printf("Input again:"); } return input; } int fac(int n) { int res = 1; int i; if (n <= 0) res = -1; for (i = 1;i <= n;i++) res *= i; return res; }
原文:https://www.cnblogs.com/abel2020/p/12973288.html