Find the sum of the series 1+11+111+1111+..... upto n terms

25 May, 2022
Here we are going to find the sum of the series 1 + 11 + 111 + 1111 +.....upto N terms (where N is given).Example :  
Input : 3
Output : 1 + 11 + 111 +....
Total sum is : 123

Input : 4
Output : 1 + 11 + 111 + 1111 +..... 
Total sum is : 1234

Input : 7
Output : 1 + 11 + 111 + 1111 + 11111 + 
         111111 + 1111111 +..... 
Total sum is : 1234567
 
Here we see that when value of N is 3, series last upto 1 + 11 + 111 i.e, three term and it’s sum is 123.Program for finding sum of above series :  
C++
C
Java
Python
C#
PHP
Javascript
// C++ program to find the sum of// the series 1+11+111+1111+....#include <bits/stdc++.h>using namespace std; // Function for finding summationint summation(int n){    int sum = 0, j = 1;    for (int i = 1; i <= n; i++) {        sum = sum + j;         // Appending a 1 at the end        j = (j * 10) + 1;    }     return sum;} // Driver Codeint main(){    int n = 5;    cout << " " <<  summation(n);    return 0;} // This code is contributed by shivanisinghss2110
// C program to find the sum of// the series 1+11+111+1111+....#include <stdio.h> // Function for finding summationint summation(int n){    int sum = 0, j = 1;    for (int i = 1; i <= n; i++) {        sum = sum + j;         // Appending a 1 at the end        j = (j * 10) + 1;    }     return sum;} // Driver Codeint main(){    int n = 5;    printf("%d", summation(n));    return 0;}
// Java program to find the sum of// the series 1+11+111+1111+....import java.io.*; class GFG{     // Function for finding summation    static int summation(int n)    {        int sum = 0, j = 1;        for (int i = 1; i <= n; i++)        {            sum = sum + j;            j = (j * 10) + 1;        }         return sum;    }     // Driver Code    public static void main(String args[])    {        int n = 5;        System.out.println(summation(n));    }} // This code is contributed// by Nikita Tiwari
# Python program to get the summation# of following seriesdef summation(n):    sum = 0    j = 1         for i in range(1, n + 1):        sum = sum + j        j = (j * 10) + 1             return sum         # Driver Coden = 5print(summation(n))
// C# program to find the sum of// the series 1+11+111+1111+....using System; class GFG{     // Function for finding summation    static int summation(int n)    {        int sum = 0, j = 1;        for (int i = 1; i <= n; i++)        {            sum = sum + j;            j = (j * 10) + 1;        }         return sum;    }     // Driver Code    public static void Main()    {        int n = 5;        Console.WriteLine(summation(n));    }} // This code is contributed by vt_m
<?php// PHP program to find the sum of// the series 1+11+111+1111+.... // Function for finding summationfunction summation($n){    $sum = 0; $j = 1;    for ($i = 1; $i <= $n; $i++)    {        $sum = $sum + $j;         // Appending a 1 at the end        $j = ($j * 10) + 1;    }     return $sum;} // Driver Code$n = 5;echo summation($n); // This code is contributed by ajit?>
<script> // Javascript program to find the sum of// the series 1+11+111+1111+....     // Function for finding summation    function summation( n) {        let sum = 0, j = 1;        for ( let i = 1; i <= n; i++) {            sum = sum + j;                         // Appending a 1 at the end            j = (j * 10) + 1;        }         return sum;    }     // Driver Code              let n = 5;        document.write(summation(n));         // This code contributed by Princi Singh </script>
Output : 
12345
Time Complexity: O(n), where n represents the given integer.Auxiliary Space: O(1), no extra space is required, so it is a constant.
Another method: Let given a series S = 1 + 11 + 111 + 1111 + . . . + upto nth term. Using formula to find sum of series. 
 
Below is the implementation of above approach. 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find the sum of// the series 1+11+111+1111+....#include <bits/stdc++.h> // Function for finding summationint summation(int n){    int sum;     sum = (pow(10, n + 1) -               10 - (9 * n)) / 81;     return sum;} // Driver Codeint main(){    int n = 5;    printf("%d", summation(n));    return 0;}
// java program to find the sum of// the series 1+11+111+1111+....import java.io.*; class GFG {     // Function for finding summation    static int summation(int n)    {        int sum;             sum = (int)(Math.pow(10, n + 1) -                10 - (9 * n)) / 81;             return sum;    }         // Driver Code    public static void main (String[] args)    {        int n = 5;        System.out.println(summation(n));    }} // This code is contributed by anuj_67.
# Python3 program to# find the sum of# the series 1+11+111+1111+....import math # Function for# finding summationdef summation(n):    return int((pow(10, n + 1) -                    10 - (9 * n)) / 81); # Driver Codeprint(summation(5)); # This code is contributed# by mits.
// C# program to find the sum of// the series 1+11+111+1111+....using System; class GFG {     // Function for finding summation    static int summation(int n)    {        int sum;             sum = (int)(Math.Pow(10, n + 1) -                10 - (9 * n)) / 81;             return sum;    }         // Driver Code    public static void Main ()    {        int n = 5;        Console.WriteLine(summation(n));    }} // This code is contributed by anuj_67.
<?php//PHP program to find the sum of// the series 1+11+111+1111+.... // Function for finding summationfunction summation($n){    $sum;     $sum = (pow(10, $n + 1) -                10 - (9 * $n)) / 81;     return $sum;} // Driver Code$n = 5;echo summation($n); // This code is contributed by aj_36?>
<script>// javascript program to find the sum of// the series 1+11+111+1111+.... // Function for finding summationfunction summation( n){    let sum;    sum = (Math.pow(10, n + 1) -               10 - (9 * n)) / 81;    return sum;} // Driver Codelet n = 5;    document.write(summation(n)) ;     // This code is contributed by aashish1995 </script>
Output :  
12345
Time Complexity: O(logn), where n represents the given integer.Auxiliary Space: O(1), no extra space is required, so it is a constant.
surya2106
jit_t
vt_m
Mithun Kumar
shivanisinghss2110
aashish1995
princi singh
samim2000
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.