#include<stdio.h>
int main()
{
int i,n;
long long s = 1; //用于存储阶乘结果,使用long long类型防止溢出
scanf("%d",&n);
if(1<=n && n<=10 )
{
for(i=1;i<=n;i++)
{
s*=i;
}
printf("%lld\n",s);// 使用%lld格式符输出long long类型
}
else
{
printf("The input value of n does not meet the requirements of the problem");
}
return 0;
}
首先 要知道“ n!=1*2*3…*n ”
这时 我们定义 i、n、s 三个变量
变量n: 认为输出数
变量i:用于 for循环
变量s:用于存储阶乘结果
其次,题目指出“ n为正整数,同时,1 <= n <= 10 ”
这时,我们需要利用 if语句对n进行判断,是否符合题目要求
if(1<=n && n<=10 )
通过则进行 n!运算
未通过则输出提示
插播一条题外话:
这是一个专栏,小伙伴们可以点击订阅专栏,会进行实时更新噢!
专栏链接:
https://blog.youkuaiyun.com/weixin_51563198/category_12890866.html?spm=1001.2014.3001.5482