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

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

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 08:53:06