Find sum of the series 1+22+333+4444+…… upto n terms

Given a number N. The task is to find the sum of the below series up to N-th term:
1 + 22 + 333 + 4444 + …up to n terms
Examples:
Input: N = 3 Output: 356 Input: N = 10 Output: 12208504795
Approach:
Below is the implementation of the above approach:
C++
// CPP program to find the sum // of given series#include <iostream>#include <math.h>using namespace std;// Function to calculate sumint findSum(int n){ // Return sum return (pow(10, n + 1) * (9 * n - 1) + 10) / pow(9, 3) - n * (n + 1) / 18;}// Driver codeint main(){ int n = 3; cout << findSum(n); return 0;} |
Java
// Java Program to find // Sum of first n termsimport java.util.*;class solution{static int calculateSum(int n){// Returning the final sumreturn ((int)Math.pow(10, n + 1) * (9 * n - 1) + 10) / (int)Math.pow(9, 3) - n * (n + 1) / 18;}// Driver codepublic static void main(String ar[]){// no. of terms to find the sumint n=3;System.out.println("Sum= "+ calculateSum(n));}}//This code is contributed by Surendra_Gangwar |
Python 3
# Python program to find the sum of given series.# Function to calculate sumdef solve_sum(n): # Return sum return (pow(10, n + 1)*(9 * n - 1)+10)/pow(9, 3)-n*(n + 1)/18# driver coden = 3print(int(solve_sum(n))) |
C#
// C# Program to find // Sum of first n termsusing System;class solution{static int calculateSum(int n){// Returning the final sumreturn ((int)Math.Pow(10, n + 1) * (9 * n - 1) + 10) / (int)Math.Pow(9, 3) - n * (n + 1) / 18;}// Driver codepublic static void Main(){// no. of terms to find the sumint n=3;Console.WriteLine("Sum= "+ calculateSum(n));}}//This code is contributed by inder_verma. |
PHP
<?php// PHP program to find the sum // of given series// Function to calculate sumfunction findSum($n){ // Return sum return (pow(10, $n + 1) * (9 * $n - 1) + 10) / pow(9, 3) - $n * ($n + 1) / 18;}// Driver code$n = 3;echo findSum($n);// This code is contributed // by inder_verma.?> |
Javascript
<script>// Javascript Program to find // Sum of first n terms function calculateSum( n) { // Returning the const sum return (parseInt(Math.pow(10, n + 1)) * (9 * n - 1) + 10) / parseInt(Math.pow(9, 3)) - n * (n + 1) / 18; } // Driver code // no. of terms to find the sum let n = 3; document.write("Sum= " + calculateSum(n));// This code is contributed by 29AjayKumar </script> |
Output:
356
Time Complexity: O(logn), where n represents the given integer, as we have used pow function.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




