设计算法并编写程序计算11^2+1+12^2+1+13^2+1+14^2+1+…+1n^2+1直到最后项小于10^-6。使用c语言带注释
#include <stdio.h>
#include <math.h>
int main() {
int n = 1; // 初始化n为1
double sum = 0, term; // 初始化sum为0,term为每一项的值
do {
term = 1.0 / (n * n + 1); // 计算每一项的值
sum += term; // 将每一项的值加到总和中
n++; // 更新n的值
} while (term >= 1e-6); // 当最后一项小于10^-6时跳出循环
printf("The sum is: %lf\n", sum); // 输出结果
return 0;
}
原文地址: http://www.cveoy.top/t/topic/dwmf 著作权归作者所有。请勿转载和采集!