C++ program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + … + n/a^n)

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to find the sum of the given series (1/a + 2/a^2 + 3/a^3 + … + n/a^n).

For this, we will be given with the value of n and our task is to add up every term starting from the first one to find the sum of the given series.

Example

#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;
}

Output

0.716049
raja
Published on 21-Nov-2019 17:43:31
Advertisements