#include<conio.h>
#include<stdio.h>
#include<string.h>
/*
8.编写函数fun,函数的功能是:根据以下公式计算s,计算结果作为函数值返回;n通过形参传入。
S=1+1/(1+2)+1/(1+2+3)+……1/(1+2+3+…+n)
例如:若n的值为11时,函数的值为1.833333。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
*/
float fun (int n)
{
int i;
float s=0,sum=0;//s:计算结果; sum:1+2+3+…+n的和
for(i=1;i<=n;i++){
sum+=i;
s+=1/sum;
}
return(s);
}
main()
{
int n;
float s;
//clrscr();
system(“cls”);
printf("\nPlease enter N:");
scanf("%d",&n);
s=fun(n);
printf(“The result is: %f\n”,s);
}
本文介绍了一个使用C语言编写的函数,该函数用于计算一个特定的数学序列S的值,其中S=1+1/(1+2)+1/(1+2+3)+...+1/(1+2+3+...+n)。通过迭代累加的方式,函数实现了对序列的精确计算,并在主函数中展示了如何输入参数n并输出计算结果。

9392

被折叠的 条评论
为什么被折叠?



