1、求阶乘 n!
int main() {
int i, n, ret;
printf("输入n的值:");
scanf("%d", &n);
ret = 1;
for (i = 1; i <= n; i++) {
ret = ret * i;
}
printf("n!= %d",ret);
return 0;
}
2、求阶乘之和 n!+(n-1)!+...+1!
int main() {
int i, j, n, ret, sum;
printf("输入的n是:");
scanf("%d", &n);
sum = 0;
for (j = 1; j <= n; j++) {
ret = 1;
for (i = 1; i <= j; i++) {
ret = ret * i;
}
sum = sum + ret;
}
printf("n!= %d\n", ret);
printf("n!+(n-1)!+...+1!= %d\n", sum);
return 0;
}
输出结果: