C++程式,用於求解級數 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n 的和
在本教程中,我們將討論一個程式,用於查詢給定級數 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n 的和。
為此,我們將獲得 n 的值,我們的任務是將從第一項開始的每一項加起來,以找到給定級數的和。
示例
#include <iostream> #include <math.h> using namespace std; //calculating the sum of the series double calc_sum(int n) { int i; double sum = 0.0, ser; for (i = 1; i <= n; i++) ser = 1/ pow(i, i); sum += ser; return sum; } int main() { int n = 5; double res = calc_sum(n); cout << res << endl; return 0; }
輸出
0.00032
廣告