Question #14082

(Computing e in c++ programming) You can approximate e by using the following series: e = 1 +1/1! +1/2! +1/3! +1/4! + ... + 1/ i! Write a program that displays the e value for i = 10000, 20000, ..., and 100000. (Hint: Since i!= i × (i − 1) × ...× 2 × 1 ,then 1/i! is 1/i(i-1)! . Initialize e and item to be 1 and keep adding a new item to e. The new item is the previous item divided by i for i = 2, 3, 4, ...)

Expert's answer

#include <iostream>

using namespace std;

void f(long double& q)

{

& long double& a,s,i;

a=1;

s=1;

& for ( i=1;i<q;i++){

a=a+1/s;

s=s*i;

}

cout.precision(15);

cout<<"e& "<<a;

&

cout<<"\n\n";

&

&

&

& }

int main()

{

cout <<"for i = 10000& \n";

f(10000);

cout <<"for i = 20000& \n";

f(20000);

&

& cout <<"for i = 30000& \n";

f(30000);

cout <<"for i = 40000& \n";

f(40000);

cout <<"for i = 50000& \n";

f(50000);

cout <<"for i = 60000& \n";

f(60000);

cout <<"for i = 70000& \n";

f(70000);

cout <<"for i = 80000& \n";

f(80000);

cout <<"for i = 90000& \n";

f(90000);

cout <<"for i = 100000& \n";

f(100000);

system("PAUSE");

}

using namespace std;

void f(long double& q)

{

& long double& a,s,i;

a=1;

s=1;

& for ( i=1;i<q;i++){

a=a+1/s;

s=s*i;

}

cout.precision(15);

cout<<"e& "<<a;

&

cout<<"\n\n";

&

&

&

& }

int main()

{

cout <<"for i = 10000& \n";

f(10000);

cout <<"for i = 20000& \n";

f(20000);

&

& cout <<"for i = 30000& \n";

f(30000);

cout <<"for i = 40000& \n";

f(40000);

cout <<"for i = 50000& \n";

f(50000);

cout <<"for i = 60000& \n";

f(60000);

cout <<"for i = 70000& \n";

f(70000);

cout <<"for i = 80000& \n";

f(80000);

cout <<"for i = 90000& \n";

f(90000);

cout <<"for i = 100000& \n";

f(100000);

system("PAUSE");

}

## Comments

## Leave a comment