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:
C++
#include <bits/stdc++.h>
using namespace std;
float sumOfSeries( int x, int k)
{
return ( float (x) / 81) * (9 * k - 1 + pow (10, (-1) * k));
}
int main()
{
int x = 9;
int k = 20;
cout << sumOfSeries(x, k);
return 0;
}
|
Java
public class GFG {
static float sumOfSeries( int x, int k)
{
float y = ( float ) ((( float )(x) / 81 ) * ( 9 * k - 1 + Math.pow( 10 , (- 1 ) * k)));
return y ;
}
public static void main (String args[]){
int x = 9 ;
int k = 20 ;
System.out.println(sumOfSeries(x, k));
}
}
|
Python3
def sumOfSeries(x, k):
return ( float (x) / 81 ) * ( 9 * k - 1 + 10 * * ( ( - 1 ) * k ) )
if __name__ = = '__main__' :
x = 9
k = 20
print (sumOfSeries(x, k))
|
C#
using System;
class GFG
{
static float sumOfSeries( int x, int k)
{
float y = ( float )((( float )(x) / 81) *
(9 * k - 1 + Math.Pow(10, (-1) * k)));
return y ;
}
public static void Main ()
{
int x = 9;
int k = 20;
Console.Write(sumOfSeries(x, k));
}
}
|
PHP
<?php
function sumOfSeries( $x , $k )
{
return (( $x ) / 81) * (9 * $k - 1 +
pow(10, (-1) * $k ));
}
$x = 9;
$k = 20;
echo sumOfSeries( $x , $k );
?>
|
Javascript
<script>
function sumOfSeries(x , k)
{
var y = (((x) / 81) * (9 * k - 1 + Math.pow(10, (-1) * k)));
return y ;
}
var x = 9;
var k = 20;
document.write(sumOfSeries(x, k));
</script>
|
Time Complexity: O(logk), where k is the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...