Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n! in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n!

For this we will be provided with the value of a. Our task is to find the sum of the given series according to the given value of a.

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
//calculating sum of given series
double sumOfSeries(double a,double num) {
   double res = 0,prev=1;
   for (int i = 1; i <= num; i++) {
      prev *= (a/i);
      res = res + prev;
   }
   return(res);
}
int main() {
   double n = 5, a=2;
   cout << sumOfSeries(a,n);
   return 0;
}

Output

6.26667
raja
Published on 27-May-2020 13:06:28
Advertisements