百度知道 - 信息提示
怎么用C语言编程求:1-1/2+1/3-1/4+...+1/99-1/100
#include int main(int argc,char *argv[]){ int i; double sum; for(sum=0.0,i=1;i<101;i++) sum += i&1 ? 1.0/i : -1.0/i; printf("The result is %g\n",sum); return 0;}
c语言 编写一个程序 1-1/2+1/3-1/4+1/5……-1/99+1/100用while语句!
#include
intmain()
{
intfm=0;
floatsum=0.0;
for(fm=1;fm<=100;fm++)
{
if(fm%2!=0)
{
sum+=1.0/fm;
}
else
{
sum-=1.0/fm;
}
}
printf("sum=%f\n",sum);
return0;
}
扩展资料
C语言计算1/2+2/3+3/4+……:的前n项和
#include
intmain(void){
inti,j,n,a;
doublef,s;
printf("请输入n的值:");
scanf("%d",&n);
i=1;
j=2;
s=0;
for(a=1;a<=n;a++)
{
f=1.0*i/j;
s+=f;
if(a!=n)
printf("%d/%d+",i,j);
else
printf("%d/%d",i,j);
i++;
j++;
}
printf("=%.2f\n",s);
system("pause");
return0;
}