递归和非递归分别实现求n的阶乘
#include<stdio.h>
#include<Windows.h>
int fact(int n)
{
if (n <= 1)
{
return 1;
}
return n*fact(n - 1);
}
int fact2(int n)
{
int res = 1;
while (n>1)
{
res *= n;
n--;
}
return res;
}
int main()
{
int res1 = fact(4);
int res2 = fact2(5);
printf("%d\n", res1);
printf("%d", res2);
system("pause");
return 0;
}