C 练习实例26

C语言教程评论

C 练习实例26

题目:利用递归方法求5!。

程序分析:递归公式:fn=fn_1*4!文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili26.html

实例

#include <stdio.h>

int main()
{
    int i;
    int fact(int);
    for(i=0;i<6;i++){
        printf("%d!=%d\n",i,fact(i));
    }
}
int fact(int j)
{
    int sum;
    if(j==0){
        sum=1;
    } else {
        sum=j*fact(j-1);
    }
    return sum;
}

以上实例输出结果为:文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili26.html

0!=1
1!=1
2!=2
3!=6
4!=24
5!=120
文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili26.html文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili26.html
运营不易,
感谢支持!
weinxin
我的微信
我的微信公众号
我的微信公众号扫一扫
weinxin
我的公众号
 最后更新:2021-12-7
公式库网
  • 本文由 公式库网 发表于 2021年12月6日20:09:36
  • 转载请务必保留本文链接:https://www.gongshiku.com/html/202112/c-lianxishili26.html

发表评论