1.关于累加法的讨论
例子1:求10!
#include<stdio.h>
int main()
{
int i, sum = 1;
for (i = 1; i <= 10; i++)
sum = sum * i;
printf("sum=%d", sum);
return 0;
}
例子2:求1+1/2+1/3…前10项
#include<stdio.h>
int main()
{
int i;
double sum = 0;
for (i = 1; i <= 10; i++)
sum = sum + 1.0 / i;
printf("%lf", sum);
return 0;
}
已知pai/4=1-1/3+1/5-1/7+1/9…,要求累加项绝对值误差小于0.000001,求pai
#include<stdio.h>
int main()
{
int i;
double sum = 0, k = 1.0;
for (i = 1; i <= 1e6; i = i + 2)
{
sum = sum + k / i;
k = -k;
}
printf("%lf", 4*sum);
return 0;
}