Hello, I am having a problem with my following lab assignment. I’ve included what I’ve completed so far. I am not sure if it is correct. If anyone can help me out, I would greatly appreciate it.
This is the problem in question:
Euler's number, "e" is used as the basic of natural logarithms. It can be approximated using the following formula:
e = 1 + 1/1! + 1/2! + 1/3! + 1/4! 1/5! + 1/6! + ... + 1/(n-1)! + 1/n!
Write a program that approximates "e" using a loop that terminates when difference between two successive values of "e" differ by less than 0.0000001.
This is what I’ve started/completed so far.
***start***
main()
{
/*repeat until euler(n) - euler(n-1)<=0.0000001 */
}
float euler(int n)
{
if(n==0)
return 1;
else
return (1/factorial(n) + euler(n-1));
}
int factorial(int n)
{
.
.
.
}
***is this correct?***
euler(0) => 1
euler(1) => 1/1! + euler(0)
1/1! + 1
euler(2) => 1/2! + euler(1)
1/2! + 1/1! + 1
euler(3) => 1/3! + euler(2)
1/3! + 1/2! + 1/1! + 1
euler(4) => 1/4! + 1/3! + 1/2! + 1/1! + 1
1/4! + euler(3)
***end***