Sum of the series 0.7, 0.77, 0.777, … upto n terms
Last Updated :
20 Feb, 2023
Given n the number of terms. Find the sum of the series 0.7, 0.77, 0.777, … upto n terms.
Examples :
Input : 2
Output : 1.46286
Input : 3
Output : 2.23609
Approach Used :
Let’s denote the sum of the series by S:
Sum = 0.7 + 0.77 + 0.777 + …. up to n terms
= 7/9(0.9 + 0.99 + 0.999 + … up to n terms)
= 7/9[(1 – 0.1) + (1 – 0.01) + (1-0.001) + … up to n terms]
= 7/9[(1 + 1 + 1… upto n terms) – (1/10 + 1/100 + 1/1000 + … upto n terms)]
= 7/9[n – 0.1 * (1 – (0.1)n)/(1 – 0.1)]
= 7/81[9n – 1 + 10-n]
Below is the Implementation to find the sum of given series:
C++
#include <bits/stdc++.h>
using namespace std;
float sumOfSeries( int n)
{
return .086 * (9 * n - 1 +
pow (10, (-1) * n));
}
int main()
{
int n = 2;
cout << sumOfSeries(n);
return 0;
}
|
Java
import java.io.*;
import java.math.*;
class GFG {
static float sumOfSeries( int n)
{
return .086f * ( 9 * n - 1 +
( float )(Math.pow( 10 , (- 1 ) * n)));
}
public static void main(String args[])
{
int n = 2 ;
System.out.println(sumOfSeries(n));
}
}
|
Python3
import math
def sumOfSeries(n) :
return . 086 * ( 9 * n - 1 + math. pow ( 10 , ( - 1 ) * n));
n = 2
print (sumOfSeries(n))
|
C#
using System;
class GFG {
static float sumOfSeries( int n)
{
return .086f * (9 * n - 1 +
( float )(Math.Pow(10, (-1) * n)));
}
public static void Main()
{
int n = 2;
Console.Write(sumOfSeries(n));
}
}
|
PHP
<?php
function sumOfSeries( $n )
{
return .086 * (9 * $n - 1 +
pow(10, (-1) * $n ));
}
$n = 2;
echo (sumOfSeries( $n ));
?>
|
Javascript
<script>
function sumOfSeries( n)
{
return .086 * (9 * n - 1 +
Math.pow(10, (-1) * n));
}
let n = 2 ;
document.write(sumOfSeries(n).toFixed(5)) ;
</script>
|
Output:
1.46286
Time Complexity: O(logn), where n is the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...