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


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

Updated on: 09-Jul-2020

94 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements