C语言题目,利用自定义函数编写程序,求下面的式子: Y=1*1/2*1/3……*1/n (n的数值由键盘输入,n<=10)

如题所述

#include <stdio.h>
int calc(int n)
{
    if (0 == n || 1 == n)
    {
        return 1;
    }
    return n * calc(n -1);
}

void main()
{
    int n = 0;
    printf("请输入n的值:\n");

    scanf("%d", &n);

    double s = 0;

    for (int i = 1; i <= n; i++)
    {
       //这里要注意下,需要转成浮点型相除,否则结果永远是0
       s = 1.0/calc(n);
    }
   
    printf("s = %lf\n", s);
}

运行结果:

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-11-09
float calc(int n)
{
int i;
float r=1;
for(i=1;i<=n;i++)
r=r*1/i;
return r;
}

void main()
{
int n;
scanf("%d",&n);
printf("\n\n%f",calc(n));
}本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网