Find the sum up to n terms of the sequence: 5 + 55 + 555 + … up to n.

**Examples :**

Input : 2 Output: 60 Input : 3 Output: 595

**Approach:**The above problem can be solved using the following formula:

Sum = 5 + 55 + 555 + …. n terms.

= 5/9[9 + 99 + 999 + …. n terms]

= 5/9[(10 – 1) + (100 – 1) + (1000 – 1) + … n terms]

= 5/9[10 + 100 + 1000 ….. – (1 + 1 + … 1)]

= 5/9[10(10^{n}– 1)/(10 – 1) + (1 + 1 + … n times))

= 50/81(10^{n}– 1) – 5n/9

Below is the Implementation to find the sum of given series:

## C++

`// C++ program for sum of the ` `// series 5 + 55 + 555.....n ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function which return the ` `// the sum of series ` `int` `sumOfSeries(` `int` `n) ` `{ ` ` ` `return` `0.6172 * ` ` ` `(` `pow` `(10, n) - 1) - ` ` ` `0.55 * n; ` ` ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 2; ` ` ` `cout << sumOfSeries(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for sum of the ` `// series 5 + 55 + 555.....n ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// function which return the ` ` ` `// the sum of series ` ` ` `static` `int` `sumOfSeries(` `int` `n) ` ` ` `{ ` ` ` `return` `(` `int` `) (` `0.6172` `* ` ` ` `(Math.pow(` `10` `, n) - ` `1` `) - ` ` ` `0.55` `* n); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String []args) ` ` ` `{ ` ` ` `int` `n = ` `2` `; ` ` ` `System.out.println(sumOfSeries(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by UPENDRA BARTWAL. ` |

*chevron_right*

*filter_none*

## Python3

`# python program for sum of the ` `# series 5 + 55 + 555.....n ` ` ` `def` `sumOfSeries(n): ` ` ` `return` `(` `int` `) (` `0.6172` `*` ` ` `(` `pow` `(` `10` `, n) ` `-` `1` `) ` `-` ` ` `0.55` `*` `n) ` ` ` ` ` `# Driver Code ` `n ` `=` `2` `print` `(sumOfSeries(n)) ` ` ` `# This code is contributed ` `# by Upendra Singh Bartwal ` |

*chevron_right*

*filter_none*

## C#

`// C# program for sum of the ` `// series 5 + 55 + 555.....n ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function which return the ` ` ` `// the sum of series ` ` ` `static` `int` `sumOfSeries(` `int` `n) ` ` ` `{ ` ` ` `return` `(` `int` `)(0.6172 * ` ` ` `(Math.Pow(10, n) - 1) - ` ` ` `0.55 * n); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 2; ` ` ` `Console.Write(sumOfSeries(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for sum of the ` `// series 5 + 55 + 555.....n ` ` ` `// function which return the ` `// the sum of series ` `function` `sumOfSeries(` `$n` `) ` `{ ` ` ` `return` `(int)(0.6172 * ` ` ` `(pow(10, ` `$n` `) - 1) - ` ` ` `0.55 * ` `$n` `); ` `} ` ` ` `// Driver code ` `$n` `= 2; ` `echo` `(sumOfSeries(` `$n` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

60

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find n terms of Fibonacci type series with given first two terms
- Sum of P terms of an AP if Mth and Nth terms are given
- Number of quadruples where the first three terms are in AP and last three terms are in GP
- Find the sum of all the terms in the n-th row of the given series
- Find the sum of the series 1+11+111+1111+..... upto n terms
- Sum of the series 0.6, 0.06, 0.006, 0.0006, ...to n terms
- Sum of the series 0.7, 0.77, 0.777, ... upto n terms
- Sum of series 2/3 - 4/5 + 6/7 - 8/9 + ------- upto n terms
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + - - - - - - upto n terms.
- Sum of first n terms of a given series 3, 6, 11, .....
- Find the sum of series 0.X + 0.XX + 0.XXX +... upto k terms
- Find the sum of n terms of the series 1,8,27,64 ....
- Find sum of the series 1+22+333+4444+...... upto n terms
- Find sum of the series ?3 + ?12 +......... upto N terms
- Find the sum of first N terms of the series 2×3 + 4×4 + 6×5 + 8×6 + ...
- Find the sum of series 3, -6, 12, -24 . . . upto N terms
- Program to find the sum of the series 23+ 45+ 75+..... upto N terms
- Sum of the first N terms of the series 5,12, 23, 38....
- Sum of the first N terms of the series 2,10, 30, 68,....
- Sum of the first N terms of the series 2, 6, 12, 20, 30....

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.