# include <stdio.h>
# include <math.h>
int main ()
{
int i=1;
double s=0.0,k=1.0,n=1.0;
while (fabs(s)>=1e-6)
{
s=s+k;
n=n+2.0;
i=-i;
k=i/n;
}
s=s*4;
printf ("s=%10.8f\n",s);
return 0;
}
运行结果为4.00000
# include <stdio.h>
# include <math.h>
int main ()
{
int i=1;
double s=0.0,k=1.0,n=1.0;
while (fabs(k)>=1e-6)
{
s=s+k;
n=n+2.0;
i=-i;
k=i/n;
}
s=s*4;
printf ("s=%10.8f\n",s);
return 0;
}