Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The value of the mathematical constant e can be expressed as an infinite series:

ID: 3631998 • Letter: T

Question

The value of the mathematical constant e can be expressed as an infinite series: e = l + 1/1! + 1/2! + 1/3! + ... This question asks you to compute an approximation of e using the above formula in two different ways. Write a program that approximates e by computing the value of 1 + 1/1! + 1/2! + 1/3! + ... + 1/n! where n is an integer entered by the user. Again, implement this program as a UNIX filter. Use a3q4a.c as the source file name. We will use the following command on bluenose to compile your program gcc - o evalue a3q4a.c Your program should NOT print anything to prompt for user input. Print an error message when the value of n is less than 1. The output should be a floating point number with exactly 5 digits after the decimal point, followed by a newline character. Therefore, when you run your program by entering ./evalue, the program will wait for your input. If you enter4, the program will output 2.70833. To test your program automatically, we will use it as a UNIX filter. The following are some examples of running the executable file: root@myunix:~/a3$ echo 3 | ./evalue 2.66667 root@myunix:~/a3$echo5 | ./evalue 2.71667 Modify the program you wrote for (a) so that the program continues adding terms until the current term (i.e. a term of the form 1/i! in the approximation formula of e) becomes less than 9, where 9 is a small (floating - point) number entered by the user. Do not add this term to your result. Again, implement this program as a UNIX filter. Usea3q4b.c as the source file name. We will use the following command on bluenose to compile your program: gcc - o evalue2 a3q4b.c Your program should NOT print anything to prompt for user input. Print an error message if the value of 9 is negative. The output should be a floating point number with exactly 5 digits after the decimal point, followed by a newline character. Therefore, when you run your program by entering ./evalue, the program will wait for your input. If you enter 0.01, the program will output 2.70833. To test your program automatically, we will use it as a UNIX filter. The following are some examples of running the executable file: root@myunix:~/csci2132/a3$ echo "0.2" | ./evalue2 2.50000 root@myunix:~/csci2132/a3$ echo "0.1" | ./evalue2 2.66667 root@myunix:~/csci2132/a3$ echo "0.001" | ./evalue2 2.71806 root@myunix:~/csci2132/a3$ echo "0.0001" | ./evalue2 2.71825

Explanation / Answer

please rate - thanks


#include <stdio.h>
int main()
{int n,f=1,i=0;
double eps;
double e=0;
scanf("%lf",&eps);
if(eps<0)
    printf("most be>0 ");
else
{   
do
{if(i!=0)
        f*=i;                             //calculating i! for all values but 0
     e=e+(1./f);                      //add new term
   i++;
}while(1./f >eps);               //is term < eps   if yes exit
printf("%.5f ",e);
}
return 0;
}