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
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP