求1-1/2+1/3-1/4+1/5……+1/99-1/100的值

/************************************
 *
 * FileName: getValue.c 
 * Creator:  Smart
 * Date:     2014-12-27 
 * 
 ************************************/
 
#include 
double getValue()
{
	double S=0;
	int i;
	for(i=1;i<=100;i++)
	{
		if(i%2)
		{
			S+=1/i;	//when i is odd number
		}
		else
		{
			S-=1/i;	//when i is even number
		}
	}
	return S;
}
int main()
{
	printf("%lf\n",getValue());
	return 0;
}

结果:

求1-1/2+1/3-1/4+1/5……+1/99-1/100的值_第1张图片

你可能感兴趣的:(Exercise)