方法一:递归实现
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int fac(int n)
{
if (1 == n)
return 1;
else
return fac(n - 1) * n;
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = fac(n);
printf("%d", ret);
return 0;
}
方法二:循环实现
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int n = 0;
scanf("%d", &n);
int sum = 1;
while (n!=0)
{
sum *= n;
n--;
}
printf("%d", sum);
return 0;
}