OK, I've spent hours looking at the posts on this equation and I still cannot figure out how to properly write the code to compute e^x after prompting user for the exponent and the desired accuracy. Could someone please help? Here is what I've tried so far...I know it is completely wrong but I don't know what else to try.
while (i <= accuracy)
{
factorial *= i;
e += 1/factorial;
i++;
while(x<=num)
{
term = term * x / factorial;
x++;
x = x+1;
total = total + term;
}
}
LB, I'm using double for the variables. Chervil, I guess that is part of my question, how would I or could I do it without nesting the loops? I came up with the following but it doesn't work either. Not sure quite how to get e^x. Any thoughts or suggestions?
for (x=1; x<= exponentX; x++)
{
factorial *= x;
ex = ex+ (pow(x, exponentX)/factorial);
}
Now that is very repetitive, and there's a simple pattern here, where each term of the series can be derived from the previous by one multiply, and one divide.
first term = 1
second term = (first term) * x / 1
third term = (second term) * x / 2
fourth term = (third term) * x / 3
fifth term = (fourth term) * x / 4
...
One comment I would make. I think it's not really appropriate to use the pow() function here, as that is effectively using the built-in version of e^{x} in its internal workings.