#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
int i = 0;
int n = 0;
int ret = 1;
scanf("%d", &n);
for (i = 1; i <=n; i++)
{
ret = ret * i;
}
printf("%d", ret);
}
以下是函数循环方式`
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int Fac1(int n)
{
int i = 0;
int ret = 1;
for (i = 1; i <= n; i++)
{
ret *= i;
}
return ret;
}
int main()
{
//求n的阶乘
int n = 0;
int ret = 0;
scanf("%d", &n);
ret = Fac1(n);
printf("%d", ret);
}
运用递归算法求解
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int Fac2(int n)
{
if (n <= 1)
return 1;
else
return n * Fac2(n - 1);
}//运用递归求解
int main()
{
//求n的阶乘
int n = 0;
int ret = 0;
scanf("%d", &n);
ret = Fac2(n);
printf("%d", ret);
}
//数学上求n的阶乘的公式
//n<=1,阶乘得1;或
//n>1,阶乘是n*(n-1)。
============================================================
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
int i = 0, ret = 1;
for (i = 1; i <= 5; i++)
{
ret = ret * i;
}
printf("%d\n", ret);
}