计算1/1-1/2+1/3-1/9+1/5 ......+ 1/99 - 1/100的值,打印出结果

方法一:

#include

int main()

{

       int i = 0;

       double sum = 0.0;

       int flag = 1;

       for(i=1;i<=100;i++)

       {

              sum+=flag*1.0/i;

              flag=-flag;

             

        }

        printf("%lf ",sum);

}

方法二:

#include

int main()

{

       int i = 0;

       int j = 0;

       double sum1 = 0.0;

       double sum2 = 0.0;

       double sum = 0.0;

       for(i=1;i<=99;i+=2)

       {

              sum1+=1.0/i;

       }

       for(j=2;j<=100;j+=2)

       {

              sum2+=1.0/j;

       }

       sum=sum1-sum2;

       printf("%lf ",sum);

}

你可能感兴趣的:(C语言练习题,c语言)