程序在C ++中找到系列1 + 1/2 + 1/3 + 1/4 + .. + 1 / n的和
在本教程中,我们将讨论一个寻找序列1+½+?的和的程序。+¼+…+1/n
为此,我们将获得n的值。我们的任务是计算达到给定值的序列之和并将其打印出来。
示例
#include <iostream>
using namespace std;
//returning the sum of given series
class sum_series {
public : double sum(int n) {
double i, s = 0.0;
for (i = 1; i <= n; i++)
s = s + 1/i;
return s;
}
};
int main() {
sum_series g;
int n = 5;
cout << "Sum is " << g.sum(n);
return 0;
}输出结果
Sum is 2.28333
热门推荐
3 送蛇的祝福语简短
4 十月简短祝福语
10 钱包祝福语简短10字
11 女生 节日祝福语简短大气
12 婚礼祝福语简短情话短句
13 公司五一祝福语简短最新
14 聚餐会祝福语简短
15 唯美的祝福语简短的
16 形容新年的祝福语简短
17 喜事敬酒祝福语简短精辟
18 领证幽默祝福语简短