C++ 程序查找级数 (1/a + 2/a^2 + 3/a^3 + … + n/a^n) 的和
在本教程中,我们将探讨一个程序,用于查找给定级数 (1/a + 2/a^2 + 3/a^3 + … + n/a^n) 的和。
为此,我们将获得 n 的值,我们的任务是将从第一个数字开始的每一项相加,以求出给定级数的和。
示例
#include <iostream>
#include <math.h>
using namespace std;
//calculating the sum of the series
float calc_sum(int a, int n) {
int i;
float sum = 0;
for (i = 1; i <= n; i++)
sum += (i/ pow(a, i));
return sum;
}
int main() {
int a = 3, n = 4;
float res = calc_sum(a,n);
cout << res << endl;
return 0;
}输出
0.716049
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP