方法一:for循环实现
程序:
#include<stdio.h>
int main()
{
double i = 0, t = 0,sum = 0,sign = -1;
for (i = 1; i <= 100; i++)
{
sign = -sign;
t =sign/ i;
sum = sum + t;
}
printf("sum=%lf\n",sum);
return 0;
}
方法二:while循环实现
程序:
#include<stdio.h>
int main()
{
double i = 1, t = 0,sum = 0,sign = -1;
while (i <= 100)
{
sign = -sign;
t =sign/ i;
sum = sum + t;
i++;
}
printf("sum=%lf\n",sum);
return 0;
}
方法三:do...while循环实现
#include<stdio.h>
int main()
{
double i = 1, t = 0,sum = 0,sign = -1;
do
{
sign = -sign;
t =sign/ i;
sum = sum + t;
i++;
}
while (i <= 100);
printf("sum=%lf\n",sum);
return 0;
}
结果:
sum=0.688172
请按任意键继续. . .
转载于:https://blog.51cto.com/yaoyaolx/1717858