1. 程式人生 > >【C語言】計算1-1/2+1/3-1/4+1/5 …… + 1/99

【C語言】計算1-1/2+1/3-1/4+1/5 …… + 1/99

//計算1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100的值
#include <stdio.h>
int main()
{
	float sum=0;
    float i;
	int n=1;
	for(i=1;i<=100;i++)
	{
		sum=sum+n/i;
		n*=-1;
	}
	printf("和為%f\n",sum);
    return 0;
}
<img src="https://img-blog.csdn.net/20150323200112023?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZG91ZG91d2ExMjM0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />