用C语言编程计算数学公式s=1⼀1*2+1⼀2*3+1⼀3*4+.......1⼀n*(n+1)

2024-11-12 06:49:26
推荐回答(3个)
回答1:

#include

void main()
{
int i, n;
float s=0.0;
scanf("%d", &n);
for (i = 1; i <= n; i ++)
s += 1.0/n/(n+1);
printf("%f\n", s);
}

回答2:

#include "stdafx.h"
#include
#include
float f(int n)
{
float s=0.0;
for(int i=1;i<=n;i++)
{
s+=(float)1/(i*(i+1));
}
return s;
}
int _tmain(int argc, _TCHAR* argv[])
{
int n;
printf("Input an integer:");
scanf("%d",&n);
printf("calculating the sum now...\n");
printf("the sum is:%f\n",f(n));
system("pause");
return 0;
}

回答3:

#include "stdafx.h"
#include
#include
float f(int n)
{
float s=0.0;
for(int i=1;i<=n;i++)
{
s+=(float)1/(i*(i+1));
}
return s;
}
int _tmain(int argc, _TCHAR* argv[])
{
int n;
printf("Input an integer:");
scanf("%d",&n);
printf("calculating the sum now...\n");
printf("the sum is:%f\n",f(n));
system("pause");
return 0;
}