Find the sum of series 0.X + 0.XX + 0.XXX +… upto k terms
Given a series of k terms, where ‘X’ is any value between 0-9 and ‘k’ is any positive integer. The task is to find the sum of given series:
0.X + 0.XX + 0.XXX +… up to ‘k’ terms
Examples:
Input: x = 4 , k = 10 Output : 4.39506 Input: x = 9 , k = 20 Output: 19.8889
Explanation:
![Rendered by QuickLaTeX.com $ Result = 0.x + 0.xx + 0.xxx + ...\, up\, to\, k\, terms\\ = x/9(0.9 + 0.99 + 0.999 +... up \, to\, k\, terms)\\ = x/9[(1 - 0.1) + (1 - 0.01) + (1-0.001) + ...\, up\, \, to \, k\, terms]\\ = x/9[(1 + 1 + 1... \, upto\, k \, terms) - (1/10 + 1/100 + 1/1000 + ...\, upto\, k \, terms)]\\ = x/9[n - 0.1 * (1 - (0.1)^k)/(1 - 0.1)]\\ = x/81[9k - 1 + (10)^{-n}]\\ $](https://web.archive.org/web/20191119123751im_/https://www.geeksforgeeks.org/wp-content/ql-cache/quicklatex.com-16a106941b1a66d9c5731287fd5c5bee_l3.png)
C++
// C++ program for sum of the series // 0.x, 0.xx, 0.xxx, ... upto k terms #include <bits/stdc++.h> using namespace std; // function which return the sum of series float sumOfSeries(int x, int k) { return (float(x) / 81) * (9 * k - 1 + pow(10, (-1) * k)); } // Driver code int main() { int x = 9; int k = 20; cout << sumOfSeries(x, k); return 0; } |
chevron_right
filter_none
Java
// Java program for sum of the series // 0.x, 0.xx, 0.xxx, ... upto k terms public class GFG { // function which return the sum of series static float sumOfSeries(int x, int k) { float y = (float) (((float)(x) / 81) * (9 * k - 1 + Math.pow(10, (-1) * k))); return y ; } // Driver code public static void main (String args[]){ int x = 9; int k = 20; System.out.println(sumOfSeries(x, k)); } // This code is contributed by ANKITRAI1 } |
chevron_right
filter_none
Python3
#Python3 program for sum of series #0.x, 0.xx, 0.xxx, ... upto k terms #function which return the sum of series def sumOfSeries(x, k): return (float(x)/81) * (9 * k - 1 + 10**( (-1)*k ) ) #Driver code if __name__=='__main__': x = 9 k = 20 print(sumOfSeries(x, k)) # This code is contributed by Shashank Sharma |
chevron_right
filter_none
C#
// C# program for sum of the series // 0.x, 0.xx, 0.xxx, ... upto k terms using System; class GFG { // function which return // the sum of series static float sumOfSeries(int x, int k) { float y = (float)(((float)(x) / 81) * (9 * k - 1 + Math.Pow(10, (-1) * k))); return y ; } // Driver code public static void Main () { int x = 9; int k = 20; Console.Write(sumOfSeries(x, k)); } } // This code is contributed // by ChitraNayal |
chevron_right
filter_none
PHP
<?php // PHP program for sum of the series // 0.x, 0.xx, 0.xxx, ... upto k terms // function which return the // sum of series function sumOfSeries($x, $k) { return (($x) / 81) * (9 * $k - 1 + pow(10, (-1) * $k)); } // Driver code $x = 9; $k = 20; echo sumOfSeries($x, $k); // This code is contributed // by inder_verma. ?> |
chevron_right
filter_none
Output:
19.8889
Recommended Posts:
- Find sum of the series ?3 + ?12 +......... upto N terms
- Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ..... upto n terms
- Find the sum of series 3, -6, 12, -24 . . . upto N terms
- Find sum of the series 1+22+333+4444+...... upto n terms
- Find the sum of the series 1+11+111+1111+..... upto n terms
- Program to find the sum of the series 23+ 45+ 75+..... 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 n terms of the series 1,8,27,64 ....
- Find the sum of first N terms of the series 2×3 + 4×4 + 6×5 + 8×6 + ...
- 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
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.



