程序在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
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志