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

• Last Updated : 09 Aug, 2022

Given two integers a and n. The task is to find the sum of the series 1/a + 2/a2 + 3/a3 + … + n/an.
Examples:

Input: a = 3, n = 3
Output: 0.6666667
The series is 1/3 + 1/9 + 1/27 which is
equal to 0.6666667
Input: a = 5, n = 4
Output: 0.31039998

Approach: Run a loop from 1 to n and get the term of the series by calculating term = (i / ai). Sum all the generated terms which is the final answer.
Below is the implementation of the above approach:

## C++

 `// C++ program to find the sum of ` `// the given series ` `#include ` `#include ` `#include `   `using` `namespace` `std;`   `// Function to return the ` `// sum of the series ` `float` `getSum(``int` `a, ``int` `n) ` `{ ` `    ``// variable to store the answer ` `    ``float` `sum = 0; ` `    ``for` `(``int` `i = 1; i <= n; ++i) ` `    ``{ `   `        ``// Math.pow(x, y) returns x^y ` `        ``sum += (i / ``pow``(a, i)); ` `    ``} ` `    ``return` `sum; ` `} `   `// Driver code ` `int` `main() ` `{` `    ``int` `a = 3, n = 3; ` `    `  `    ``// Print the sum of the series ` `    ``cout << (getSum(a, n)); ` `    ``return` `0;` `}`   `// This code is contributed ` `// by Sach_Code`

## Java

 `// Java program to find the sum of the given series` `import` `java.util.Scanner;`   `public` `class` `HelloWorld {`   `    ``// Function to return the sum of the series` `    ``public` `static` `float` `getSum(``int` `a, ``int` `n)` `    ``{` `        ``// variable to store the answer` `        ``float` `sum = ``0``;` `        ``for` `(``int` `i = ``1``; i <= n; ++i) {`   `            ``// Math.pow(x, y) returns x^y` `            ``sum += (i / Math.pow(a, i));` `        ``}` `        ``return` `sum;` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `a = ``3``, n = ``3``;`   `        ``// Print the sum of the series` `        ``System.out.println(getSum(a, n));` `    ``}` `}`

## Python 3

 `# Python 3 program to find the sum of ` `# the given series ` `import` `math`   `# Function to return the ` `# sum of the series ` `def` `getSum(a, n):`   `    ``# variable to store the answer ` `    ``sum` `=` `0``; ` `    ``for` `i ``in` `range` `(``1``, n ``+` `1``):` `    `  `        ``# Math.pow(x, y) returns x^y ` `        ``sum` `+``=` `(i ``/` `math.``pow``(a, i)); ` `        `  `    ``return` `sum``; `   `# Driver code ` `a ``=` `3``; n ``=` `3``; ` `    `  `# Print the sum of the series ` `print``(getSum(a, n)); `   `# This code is contributed ` `# by Akanksha Rai`

## C#

 `// C# program to find the sum ` `// of the given series` `using` `System;`   `class` `GFG` `{` `    `  `// Function to return the sum` `// of the series` `public` `static` `double` `getSum(``int` `a, ``int` `n)` `{` `    ``// variable to store the answer` `    ``double` `sum = 0;` `    ``for` `(``int` `i = 1; i <= n; ++i) ` `    ``{`   `        ``// Math.pow(x, y) returns x^y` `        ``sum += (i / Math.Pow(a, i));` `    ``}` `    ``return` `sum;` `}`   `// Driver code` `static` `public` `void` `Main ()` `{` `    ``int` `a = 3, n = 3;`   `    ``// Print the sum of the series` `    ``Console.WriteLine(getSum(a, n));` `}` `}`   `// This code is contributed by jit_t`

## PHP

 ``

## Javascript

 ``

Output:

`0.6666667`

Time Complexity: O(nlogn)

Auxiliary Space: O(1)

Method: Finding the sum of series without using pow function

## C++

 `#include ` `#include` `using` `namespace` `std;`   `int` `main()` `{`   `  ``// sum of series` `  ``int` `n = 3, a = 3;` `  ``double` `s = 0;`   `  ``// iterating for loop n times` `  ``for` `(``int` `i = 1; i < n + 1; i++) ` `  ``{`   `    ``// finding sum ` `    ``s = s + (i / (``pow``(a, i)));` `  ``}`   `  ``// printing the result` `  ``cout << s;` `  ``return` `0;` `}`   `// This code is contributed by ksrikanth0498.`

## Java

 `/*package whatever //do not write package name here */` `// Java code to print ` `// sum of series` `import` `java.io.*;`   `class` `GFG {` `    `  `    ``public` `static` `void` `main (String[] args) {` `        ``int` `n = ``3``,a = ``3``,s = ``0``;` `        ``//iterating for loop n times` `        ``for` `(``int` `i = ``1``; i <= n; i++) {` `            ``//finding sum ` `            ``s = s + (i/(a**i))         ` `          ``}` `        ``//printing the result` `        ``System.out.println(s);` `    ``}` `}` `// This code is contributed by Atul_kumar_Shrivastava`

## Python3

 `# Python code to print ` `# sum of series` `n ``=` `3``; a ``=` `3``; s ``=` `0`   `# iterating for loop n times` `for` `i ``in` `range``(``1``, n ``+` `1``): ` `  `  `  ``# finding sum ` `  ``s ``=` `s ``+` `(i``/``(a``*``*``i))` `  `  `# printing the result` `print``(s)`   `# this code is contributed by Gangarajula Laxmi`

## C#

 `using` `System;` `public` `class` `GFG` `{`   `    ``static` `public` `void` `Main ()` `    ``{`   `        ``// sum of series` `        ``int` `n = 3, a = 3, s = 0;` `      `  `        ``// iterating for loop n times` `        ``for` `(``int` `i = 1; i < n + 1; i++) ` `        ``{` `          `  `            ``// finding sum ` `            ``s = s + (i / (Math.Pow(a, i)));` `        ``}` `      `  `        ``// printing the result` `        ``Console.WriteLine(s);  ` `    ``}`   `//   this code is contributed by Gangarajula Laxmi`

## Javascript

 ``

Output

`0.6666666666666667`

Time complexity: O(nlogn) since a single using for loop and logn for inbuilt pow() function.

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles