递归和非递归分别实现求n的阶乘
作者:互联网
1.递归
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int input(int n)
{
if (n == 1)
{
return 1;
}
return n*input(n - 1);
}
int main()
{
int n = 1;
printf("输入一个数");
scanf("%d",& n);
printf("%d", input(n));
return 0;
}
2.非递归
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int input(int n)
{
int a = 1;
int i = 1;
for (i = 1; i <= n; i++)
a*=i;
return a;
}
int main()
{
int n = 1;
printf("输入一个数");
scanf("%d", &n);
printf("%d", input(n));
return 0;
}
标签:include,return,SECURE,递归,int,阶乘,input,分别 来源: https://blog.csdn.net/weixin_45231718/article/details/99087086