Sum of the series 0.6, 0.06, 0.006, 0.0006, …to n terms

Given the number of terms i.e. n. Find the sum of the series 0.6, 0.06, 0.006, 0.0006, …to n terms.

Examples:

Input : 2
Output : 0.65934

Input : 3
Output : 0.665334



Lets denote the sum by S:
Using the formula S_{n}=\frac{a\left ( 1-r^{n} \right )}{1-r}, we have [since r<1]

S_{n}=\frac{\frac{6}{10}\left \{ 1-\left ( \frac{1}{10}  \right )^n\right \}}{1-\frac{1}{10}}

S_{n}=\frac{6}{9}\left ( 1-\frac{1}{10^n} \right )

S_{n}=\frac{2}{3}\left ( 1-\frac{1}{10^n} \right )

Hence the required sum is S_{n}=\frac{2}{3}\left ( 1-\frac{1}{10^n} \right )

Below is the implementation:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find sum of 0.6, 0.06,
// 0.006, 0.0006, ...to n terms
#include <bits/stdc++.h>
using namespace std;
  
// function which return the
// the sum of series
float sumOfSeries(int n)
{
    return (0.666) * (1 - 1 / pow(10, n));
}
  
// Driver code
int main()
{
    int n = 2;
    cout << sumOfSeries(n);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// java program to find sum of 0.6, 0.06,
// 0.006, 0.0006, ...to n terms
import java.io.*;
  
class GFG 
{
    // function which return the
    // the sum of series
    static double sumOfSeries(int n)
    {
        return (0.666) * (1 - 1 /Math. pow(10, n));
    }
      
      
    // Driver code
    public static void main (String[] args) 
    {
        int n = 2;
        System.out.println ( sumOfSeries(n));
          
    }
}
  
// This code is contributed by vt_m

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find 
# sum of 0.6, 0.06, 0.006, 
# 0.0006, ...to n terms
import math
  
# function which return 
# the sum of series
def sumOfSeries(n):
    return ((0.666) * 
            (1 - 1 / pow(10, n)));
  
# Driver code
n = 2;
print(sumOfSeries(n));
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find sum of 0.6, 0.06,
// 0.006, 0.0006, ...to n terms
using System;
  
class GFG {
      
    // function which return the
    // the sum of series
    static double sumOfSeries(int n)
    {
        return (0.666) * (1 - 1 /Math. Pow(10, n));
    }
      
    // Driver code
    public static void Main () 
    {
        int n = 2;
          
        Console.WriteLine( sumOfSeries(n));
          
    }
}
  
// This code is contributed by vt_m

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find sum of 0.6, 0.06,
// 0.006, 0.0006, ...to n terms
  
// function which return the
// the sum of series
function sumOfSeries($n)
{
    return (0.666) * (1 - 1 / 
                pow(10, $n));
}
  
// Driver code
$n = 2;
echo(sumOfSeries($n));
  
// This code is contributed by Ajit.
?>

chevron_right



Output:

0.65934


My Personal Notes arrow_drop_up

Intern at GeeksforGeeks

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.



Improved By : jit_t, Mithun Kumar