用C语言编写程序计算并输出数列1-1⼀3+1⼀5-1⼀7+1⼀9-1⼀11+·····+1⼀101的值。

2024-11-27 10:44:09
推荐回答(3个)
回答1:

#include
#include
int main(){
double result=0;
double s=1;
int count=1;
do{
if(count%2==1)
{
result+=1/s;
s+=2;
count++;
}
else
{
result-=1/s;
s+=2;
count++;
}
}while(s <= 101);
printf("1-1/3+1/5-1/7+1/9-1/11+·····+1/101=%f\n", result);
system("pause");
return 1;
}

回答2:

#include
main()
{
int i;
float a=1.0,s=0;
for(i=1;i<=101;i+=2)
{
s=s+a/i;
a=-a;
}
printf("%8.5f",s);
getchar();
}

回答3:

请问是输出分数形式,还是整型,还是浮点型的?