#include
double calculation(int n)
{
int fz = 0;//分子
int fm = 1;//分母
int i,k=1;
//求分母
for(i=0;ifm *= k;
k+=2;
}
//求分子
for(i=0,k=1;ifz += fm/k;
k+=2;
}
printf("%d,%d\n",fz,fm);
return fz*1.0/fm;
}
int main()
{
int n;
printf("请输入N:");
scanf("%d",&n);
printf("%f\n",calculation(n));
}
统分了再计算
#include
int main(void)
{
int n;
float s=0.0;
printf("请输入需要计算数列前多少项:");
while(!scanf("%d",&n)){
printf("输入错误!请重新输入:");
fflush(stdin);
}
for(int i=0;i
}
printf("数列前 %d 项之和为:%g\n",n,s);
return 0;
}
#include
int main()
{
int n,i;
float sum=0.0;
scanf("%d",&n);
if(n <= 0) //判断输入的合法性
{
printf("Input Error!\n");
return -1;
}
for(i=1;i<=n;i++)
{
sum +=1.0/( 2*i-1);
}
printf( "sum: %.3f\n",sum);
return 0;
}
示例运行结果:
20
sum: 2.480
#include
int
main(void)
{
int
n;
float
s=0.0;
printf("请输入需要计算数列前多少项:");
while(!scanf("%d",&n)){
printf("输入错误!请重新输入:");
fflush(stdin);
}
for(int
i=0;i
}
printf("数列前
%d
项之和为:%g\n",n,s);
return
0;
}
#include "stdio.h"
int main()
{
int n = 0, i = 0;
float sum = 0;
scanf( "%d", &n );
for ( i = 1; i <= n; i++ )
{
sum += 1.0 / ( i * 2 - 1 );
}
printf( "sum: %0.2f\n", sum );
return 0;
}