需要用到for循环语句,观察分母是由1~100变化,而系数是正负交替的
pow(a,b)函数,a的b次方,注意整体元素的数据类型,是浮点型。
程序代码如下:
#include <stdio.h>
#include <Windows.h>
#include <math.h>
int main()
{
float i;
float k;
i = 0.0;
k = 0.0;
float count = 0.0;
for (i = 1; i <= 100; i++)
{
k = (pow(-1, i + 1))/ i;
count = count + k; //每次得到结果加入count中
}
printf("%f", count); //输出count
system("pause");
return 0;
}
运行结果如图: