I know this has been asked before, but none of the threads I found helped me much. I am a beginner at C++ programming and need help figuring out why this doesn't work.
Question: e^x can be approximated by sum
1 + x + x^2/2! + x^3/3 + ... + x^n/n!
Write a program that takes a value x as input and outputs this sum for n taken to be each of the values 1 to 100. The program should also output e^x calculated using the predefined function exp.
Use variables of type double to store the factorials or you are likely to produce integer overflow. You might want to output 10 lines with 10 values on each line.
My teacher prefers us to use Borland, although I also have Dev on my computer.
#include <iostream.h>
#include <conio.h>
#include <math.h>
main ()
{
int x;
double sum, fact;
char ans;
do
{
cout << "Please enter the value x: ";
cin >> x;
cout << endl;
sum = fact = 1;
for (int n=1; n<=100; n++)
{
for (int i=0; i<n; i++)
{
fact = n*fact;
}
sum += (pow(x,n)/fact);
cout <<"n = "<<n<<" and sum = "<< sum << " and
exp("<<x<<") = "<<exp(x)<<endl;
}
cout << endl;
cout << "Do you want to calculate this again? (y/n): ";
cin >> ans;
} while (ans == 'y' || ans == 'Y');
getch ();
}