Sum of the series 5+55+555+.. up to n terms Last Updated : 20 Feb, 2023 Comments Improve Suggest changes Like Article Like Report 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(10n - 1)/(10 - 1) + (1 + 1 + ... n times)) = 50/81(10n - 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; } 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. 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 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. 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. ?> JavaScript <script> // javascript program for sum of the // series 5 + 55 + 555.....n // function which return the // the sum of series function sumOfSeries(n) { return parseInt( (0.6172 * (Math.pow(10, n) - 1) - 0.55 * n)); } // Driver code var n = 2; document.write(sumOfSeries(n)); // This code is contributed by aashish1995 </script> Output : 60 Time complexity: O(log n) since using the inbuilt power function. Auxiliary Space: O(1) Create Quiz Comment P Prateek Bajaj Follow 0 Improve P Prateek Bajaj Follow 0 Improve Article Tags : Misc Mathematical DSA Basic Coding Problems series series-sum +2 More Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 1 min read Problem of The Day - Develop the Habit of Coding 5 min read Like