# Find geometric sum of the series using recursion

Given an integer N we need to find the geometric sum of the following series using recursion.

1 + 1/3 + 1/9 + 1/27 + … + 1/(3^n)

Examples:

```Input N = 5
Output: 1.49794

Input: N = 7
Output: 1.49977
```

Approach:

In the above-mentioned problem, we are asked to use recursion. We will calculate the last term and call recursion on the remaining n-1 terms each time. The final sum returned is the result.

Below is the implementation of the above approach:

## C++

 `// CPP implementation to Find the ` `// geometric sum of the series using recursion ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// function to find the sum of given series ` `double` `sum(``int` `n) ` `{ ` `    ``// base case ` `    ``if` `(n == 0) ` `        ``return` `1; ` ` `  `    ``// calculate the sum each time ` `    ``double` `ans = 1 / (``double``)``pow``(3, n) + sum(n - 1); ` ` `  `    ``// return final answer ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``// integer initialisation ` `    ``int` `n = 5; ` ` `  `    ``cout << sum(n) << endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// JAVA implementation to Find the ` `// geometric sum of the series using recursion ` ` `  `import` `java.util.*; ` ` `  `class` `GFG { ` ` `  `    ``static` `double` `sum(``int` `n) ` `    ``{ ` `        ``// base case ` `        ``if` `(n == ``0``) ` `            ``return` `1``; ` ` `  `        ``// calculate the sum each time ` `        ``double` `ans = ``1` `/ (``double``)Math.pow(``3``, n) + sum(n - ``1``); ` ` `  `        ``// return final answer ` `        ``return` `ans; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``// integer initialisation ` `        ``int` `n = ``5``; ` ` `  `        ``// print result ` `        ``System.out.println(sum(n)); ` `    ``} ` `} `

## Python3

 `# CPP implementation to Find the  ` `# geometric sum of the series using recursion ` ` `  ` `  `def` `sum``(n): ` `     `  `    ``# base case  ` `    ``if` `n ``=``=` `0``: ` `        ``return` `1` `     `  `    ``# calculate the sum each time ` `    ``# and return final answer ` `    ``return` `1` `/` `pow``(``3``, n) ``+` `sum``(n``-``1``) ` ` `  `n ``=` `5``; ` ` `  `print``(``sum``(n)); `

## C#

 `// C# implementation to Find the ` `// geometric sum of the series using recursion ` ` `  `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``static` `double` `sum(``int` `n) ` `    ``{ ` `        ``// base case ` `        ``if` `(n == 0) ` `            ``return` `1; ` ` `  `        ``// calculate the sum each time ` `        ``double` `ans = 1 / (``double``)Math.Pow(3, n) + sum(n - 1); ` ` `  `        ``// return final answer ` `        ``return` `ans; ` `    ``} ` ` `  `    ``// Driver code ` `    ``static` `public` `void` `Main() ` `    ``{ ` `        ``int` `n = 5; ` ` `  `        ``Console.WriteLine(sum(n)); ` `    ``} ` `} `

Output:

```1.49794
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.