Need to solve it using code blocks for c for engineers Use a while loop to appro
ID: 3841993 • Letter: N
Question
Need to solve it using code blocks for c for engineers Use a while loop to approximate the value of e^x up to n=10 using the formula below. After each iteration, print the updated terms using a tabular format illustrated below n, x^n, n!, e^x. The factorial of a nonnegative integer n is written as n! and is defined as n! = n(n -1) (n -2) ...1. Factorials are not built into C programming but can be calculated efficiently in the loop body. By definition, o!=1 and 1! = 1. For example, 5! = s*4*3*2*1 which yield 120 e^x = 1 + x/11 + x^2/2! + x^3/3! + ....+x^n/n! Your output should resemble the following for x=1/5. The data is neatly formatted in columns using the escape sequence. The approximation is most accurate for values nearest to zero.Explanation / Answer
#include <stdio.h>
float eApprox(float x, int maxIter) {
/* Initializing for first term */
int n=1;
float xN = 1;
int nFact = 1;
float eX = 1;
float prevTerm = 1;
printf(" n x^n n! e^x ");
printf("%2d %2.6f %-10d %2.6f ", 0, xN, nFact, eX);
/* from n=1 to n=10 */
while (n <= maxIter) {
float curTerm = prevTerm * (x/n);
eX += curTerm;
xN *= x;
nFact *= n;
printf("%2d %2.6f %-10d %2.6f ", n, xN, nFact, eX);
prevTerm = curTerm;
n++;
}
printf(" e^%.6f = %.6f (approximated upto n = %d) ",x, eX, maxIter);
return eX;
}
int main()
{
eApprox(1.5, 10);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.