/******************************************************************************
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, pi = 3.1415926535;
cout << "This program uses a Taylor series to approximate SNCDF"
<< endl << "from negative infinity to some inputted x.";
while (degree != 0)
{
cout << endl << endl << "Enter a degree: ";
cin >> degree;
if (degree != 0)
{
cout << "Enter an x-value: ";
cin >> x;
if (x > 2.5)
{
cout << "Approximation: 1";
}
else if (x < -2.5)
{
cout << "Approximation: 0";
}
else
{
for (n = 0, taylor = 0; n <= degree; n++)
{
taylor = taylor + pow (-0.5, n) * pow (x, 2 * n + 1)
/ factorial (n) / (2 * n + 1);
}
cout << "Approximation: " << taylor / sqrt (2 * pi) + 0.5;
}
}
}
}
int
factorial (int a)
{
int b = 1;
for (b = 1; a > 1; a--)
{
b = b * a;
}
return b;
}