Find the sum of the series 1+11+111+1111+….. upto n terms
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 program to find the sum of // the series 1+11+111+1111+.... #include <stdio.h> // Function for finding summation int 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 Code int main() { int n = 5; printf("%d", summation(n)); return 0; } |
Java
// 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
# Python program to get the summation # of following series def summation(n): sum = 0 j = 1 for i in range(1, n + 1): sum = sum + j j = (j * 10) + 1 return sum # Driver Code n = 5print(summation(n)) |
C#
// 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 // PHP program to find the sum of // the series 1+11+111+1111+.... // Function for finding summation function 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 ?> |
Output :
12345
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++
// C++ program to find the sum of // the series 1+11+111+1111+.... #include <bits/stdc++.h> // Function for finding summation int summation(int n) { int sum; sum = (pow(10, n + 1) - 10 - (9 * n)) / 81; return sum; } // Driver Code int main() { int n = 5; printf("%d", summation(n)); return 0; } |
Java
// 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
# Python3 program to # find the sum of # the series 1+11+111+1111+.... import math # Function for # finding summation def summation(n): return int((pow(10, n + 1) - 10 - (9 * n)) / 81); # Driver Code print(summation(5)); # This code is contributed # by mits. |
C#
// 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 //PHP program to find the sum of // the series 1+11+111+1111+.... // Function for finding summation function 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 ?> |
Output :
12345
Recommended Posts:
- Find sum of the series ?3 + ?12 +......... upto N terms
- Find the sum of series 3, -6, 12, -24 . . . upto N terms
- Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ..... upto n terms
- Find the sum of series 0.X + 0.XX + 0.XXX +... upto k terms
- Program to find the sum of the series 23+ 45+ 75+..... upto N terms
- Find sum of the series 1+22+333+4444+...... 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 the series (1*2) + (2*3) + (3*4) + ...... upto n terms
- Sum of the series 0.7, 0.77, 0.777, ... upto n terms
- Find n terms of Fibonacci type series with given first two terms
- Find the sum of first N terms of the series 2*3*5, 3*5*7, 4*7*9, ...
- Find the sum of all the terms in the n-th row of the given series
- Find the sum of first N terms of the series 2×3 + 4×4 + 6×5 + 8×6 + ...
- Find the sum of n terms of the series 1,8,27,64 ....
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.



