Skip to content

Instantly share code, notes, and snippets.

@catharsis96
Created October 22, 2016 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catharsis96/86d2eb5eddf9fab1a3ac88cb67fabae0 to your computer and use it in GitHub Desktop.
Save catharsis96/86d2eb5eddf9fab1a3ac88cb67fabae0 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
using namespace std;
int ComputeFactorial(int number) {
int fact = 0;
for (int j = 1; j <= number; j++) {
fact = fact * j;
}
return fact;
}
double ComputeSeriesValue(double x, int n) {
double seriesValue = 0.0;
double xpow = 1;
for (int k = 0; k <= n; k++) {
seriesValue += xpow / ComputeFactorial(k);
xpow = xpow * x;
}
return seriesValue;
}
int main() {
cout << "This program is used to compute the value of the following series : " << endl;
cout << "(x^0)/0! + (x^1)/1! + (x^2)/2! + (x^3)/3! + (x^4)/4! + ........ + (x^n)/n! " << endl;
cout << "Please enter the value of x : " ;
double x;
cin >> x;
int n;
cout << endl << "Please enter an integer value for n : " ;
cin >> n;
cout << endl;
double seriesValue = ComputeSeriesValue(x, n);
cout << "The value of the series for the values entered is "
<< seriesValue << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment