Sum of the series 0.7, 0.77, 0.777 … upto n terms in C++

C++Server Side ProgrammingProgramming

In this problem, we are given n terms of a number. The series is 0.7, 0.77, 0.777…. Our task is to create a program to find the sim of the series 0.7, 0.77, 0.777 … upto n terms.

Let’s take an example to understand the problem,

Input  4

Output  

Explanation −0.7 + 0.77 + 0.777 + 0.7777 = 3.0247

To solve this problem, we will derive the formula for sum of series. Let’s find the general formula for it,

sum = 0.7 + 0.77 + 0.777 + ... upto n terms
sum = 7 (0.1 + 0.11 + 0.111 + … upto n terms)
sum = 7 (9/9)(0.1 + 0.11 + 0.111 + … upto n terms)
sum = 7/9(0.9 + 0.99 + 0.999 + … upto n terms)
sum = 7/9 ( (1 - 0.1) + (1 - 0.01) + (1 - 0.001) + … upto n terms )
sum = 7/9 ( (1+ 1 + 1 + … + upto n) - (0.1 + 0.01 + 0.001 + … upto n terms)
)
sum = 7/9 ( (n) - (1/10 + 1/100 + 1/1000 + … upto n terms) )
sum = 7/9 ( n - 0.1 * ((1 - (0.1)n)/ (1 - 0.1)) )
sum = 7/9 ( n - 0.1 * ((1 - (0.1)n)/ (0.9)) )
sum = 7/9 ( n - ((1 - (1/10n) )/9) )
sum = 7/81 ( 9n - (1 - (1/10n) ) )
sum = 7/81 (9n - 1 + 10-n)

This formula given the general formula for the sum of series upto n terms.

Example

Program to illustrate the working of our solution,

 Live Demo

#include <iostream>
#include <math.h>
using namespace std;
float calcSeriesSum(int n) {
   return ( (.08641) * (9*n - 1) + pow(10, (-1) * n) );
}
int main() {
   int n = 5;
   cout<<"The sum of series 0.7, 0.77, 0.777, ... upto n terms is "<<calcSeriesSum(n);
   return 0;
}

Output

The sum of series 0.7, 0.77, 0.777, ... upto n terms is 3.80205
raja
Published on 14-Aug-2020 13:27:06
Advertisements