用递归的方法计算n的阶乘
#include<stdio.h>
int main()
{
int n = 0;
int mul = 1;
scanf("%d",&n);
for(int i = 1; i <= n; i++)
{
mul = mul * i;
}
printf("%d\n",mul);
return 0;
}
以上代码是直接算n的阶乘
#include<stdio.h>
int Mul(n)
{
int i = 0;
int mul = 1;
for (i = 1; i <= n; i++)
{
mul = mul*i;
}
return mul;
}
int main()
{
int n = 0;
int mul = 0;
scanf("%d",&n);
mul = Mul(n);
printf("%d\n",mul);
return 0;
}
这里是利用函数实现计算n的阶乘
#include<stdio.h>
int Mul(n)
{
for (int i = 1; i <= n; i++)
{
if (n <= 1)
return 1;
else
return n*Mul(n-1);
}
}
int main()
{
int n = 0;
int mul = 0;
scanf("%d",&n);
mul = Mul(n);
printf("%d\n",mul);
return 0;
}
这里是通过递归实现计算n的阶乘
标签:递归,int,C语言,学习,mul,阶乘,Mul,include,第十三天 From: https://blog.51cto.com/u_16263798/7644597