Monday, January 13, 2025
Google search engine
HomeData Modelling & AIFind the sum of series 0.X + 0.XX + 0.XXX +… upto...

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: 
$ 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}]\\ $
 

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;
}


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
}


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


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


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.
?>


Javascript




<script>
 
// javascript 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)
{
   var y =  (((x) / 81) * (9 * k - 1 + Math.pow(10, (-1) * k)));
   return y ;
}
 
// Driver code
 
 var x = 9;
 var k = 20;
 document.write(sumOfSeries(x, k));
 
// This code contributed by shikhasingrajput
 
</script>


Output: 

19.8889

 

Time Complexity: O(logk), where k is the given integer.

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 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments