C++用fun函数编写求1⼀1*2+1⼀2*3+1⼀3*4+.......+1⼀n(n+1)

2024-12-12 06:44:24
推荐回答(2个)
回答1:

#include
float fun(int n)
{
float sum=0;
for(float i=1;i<=n;i++)
{
float f=1/(i*(i+1));
sum=sum+f;
}
return sum;
}
void main()
{
int a=5;
cout<
}

回答2:

#include
#include
double fun(int n)
{
int i;
double sum = 0.0;
double temp;
for(i = 1;i<=n;i++)
{
temp = i*(i+1);
sum += 1/temp;
}
return sum;
}
int main()
{
int n;
printf("input n:\n");
scanf("%d",&n);
double re = fun(n);
printf("result = %f",re);
// system("pause");
return 0;
}