方法1
#include<stdio.h>
int main()
{
int n = 0;
printf("请输入n的值:");
scanf_s("%d",&n);
int sz = 1;//sz!=0
int i = 1;//循环里面i!=0,否则循环没有意义
for ( i = 1; i <=n; i++)
{
sz *= i;
}
printf("阶乘为:%d\n",sz);
return 0;
}
运行结果
方法2
#include<stdio.h>
int main()
{
//n的阶乘
int n;
printf("请输入n的值:");
while (scanf_s("%d",&n))
{
int sz = 1;
int i = 0;
for ( i = 1; i <= n; i++)
{
sz = sz*i;
}
printf("阶乘为:%d\n", sz);
}
return 0;
}
运行结果
希望可以帮助到有需要的人!!!