Write a C program that
Scan a double variable x and evaluate y=exp(x) by using the math.h library of functions.
Your objective in this homework is to compare the so calculated value of y with the approximate value Y obtained by using 1, 2, 3, 4, and 5 leading terms of the Taylor series:
exp(x) = x^0/0! + x^1/1! + x^2/2! + x^3/3! + x^4/4! + ....
Recall that k!=k*(k-1)*(k-2)* ... *3*2*1 and 0!=1.
Use a do/while loop in conjunction with a switch statement switch(). Enter a character 1 for one term approximation,2 for two terms, etc., by using getchar() function. Use 0 to exit the program. Within switch, use default:
if (char != '0') => unrecognized operator.
Let your opening case calculate the fourth term x^4/4!, let the subsequent case calculate the x^3/3! term, etc. (In this way, you wan't need to use repeatedly a break statement within your switch).
Evaluate the corresponding Y, print the result in one line,and the relative error (Y-y)/y in another line.
Execute your program for all five cases (with corresponding
printouts).
............................................
Your output should be like this:
Enter x: 1.25
True value of exp(1.2500) = 3.490343
Enter a character 1-5 (0 to exit):
1
1 term(s) approximation
Approximate exp(1.2500) = 1.000000
Relative error = -71.349520 percent
Enter a character 1-5 (0 to exit):
2
2 term(s) approximation
Approximate exp(1.2500) = 2.250000
Relative error = -35.536421 percent
Enter a character 1-5 (0 to exit):
3
3 term(s) approximation
Approximate exp(1.2500) = 3.031250
Relative error = -13.153233 percent
Enter a character 1-5 (0 to exit):
4
4 term(s) approximation
Approximate exp(1.2500) = 3.356771
Relative error = -3.826905 percent
Enter a character 1-5 (0 to exit):
5
5 term(s) approximation
Approximate exp(1.2500) = 3.458496
Relative error = -0.912428 percent
Enter a character 1-5 (0 to exit):
6
unrecognized operator
Enter a character 1-5 (0 to exit):
0