/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <cmath>
#include <iostream>
using namespace std;
int factorial (int a);
int main ()
{
long double x = 1, degree = 1, e = 2.718281828459045, n = 0, taylor = 0;
cout << "This program uses a Taylor series to approximate e^x"
<< endl << "at some inputted x.";
while (degree != 0)
{
cout << endl << endl << "Enter a degree: ";
cin >> degree;
if (degree != 0)
{
cout << "Enter an x-value: ";
cin >> x;
for (n = 0, taylor = 0; n <= degree; n++)
{
taylor = taylor + pow (x, n) / factorial (n);
}
cout << "Approximation: " << taylor << endl;
cout << "Error (%): " << taylor / pow (e, x) - 1;
}
}
}
int factorial (int a)
{
int b = 1;
for (b = 1; a > 1; a--)
{
b = b * a;
}
return b;
}