# C++ Program to Estimate value of 'e' and e^x

This is a simple code for the program to estimate value of e and e^x (e power x) using factorials. It is coded by building on three different programs.

- The first part finds the factorial of a given number.
- The second one incorporates this factorial code to estimate the value of 'e'.
- The third program incorporates the second one with some editions to estimate the value of e^x.

## C++ Program to find Factorial of a given Number

#include <iostream> using namespace std; int main () { int A, B, D=1; cout<<"Enter The Integer\n"; cin>>A; for (B=1; B<=A ; B++ ) { D = D * B; } cout<<"Factorial is : "<<D<<endl; system("pause"); }

## Logic:

We have to multiply the given number whose factorial is going to be computed by all the numbers less then that number. To do this we will be using a for loop and its condition will be set at that given number.

**Line 6:** All necessary variables are initialized, it is necessary to set D as 1 initially.

**Line 9:** Asks us to input that number whose factorial is to be found. It is stored in variable A.

**Line 11:** Initiates a for loop with A as its condition.

**Line 13:** This is the most important step. Due to the for loop B will have all the values from 1 till A and its value will increase by 1 each time the loop runs. D was already initialized as 1.

**Now Understand this bit**, first time the loop runs B is 2 it is multiplied by D which was 1 and the answer 2 is stored again in D. When the loop runs next time B is 3 and D is 2, they both are multiplied and result 6 is stored in D. It goes on like this until loop terminates and at that times D gives the value of the factorial.

## C++ Program to estimate the value of 'e'

#include <iostream> #include <iomanip> using namespace std; int main () { int A, B; double E, D; for (A=1; A<=15; A++ ) { D=1; for (B=1; B<=A ; B++ ) { D = D * B; } E = E + 1 / D; } cout<<setprecision(10)<<"Estimated Value of e is "<< E + 1 <<endl; system("pause");

We will use the formula **1 + 1/1! + 1/2! + 1/3!...** to estimate value of 'e' so see, you have to use the factorial in this computation and hence the C++ code for finding factorial given above will be incorporated into it.

## Logic:

Note that the formula uses factorials of numbers which are increasing by a factor of 1 so just simply put the factorial code you wrote earlier into another for loop, the rest is just simple maths and the job is done.

**Line 2:** Includes the <iomanip> library file which includes the 'set precision' function.

**Line 8:** Now it is important to initialize our variables as 'doubles' because now calculation will involve decimal values.

**Line 10:** Initiates a for loop to calculate factorials from 1 to 15.

**Line 12-16:** The code for finding factorial is incorporated here. It becomes a nested for statement.

**Line 17:** Performs other calculation according to the formula.

**Line 19:** Sets the precision of the answer to 10 decimal places and prints the value of e which was stored in variable E. (we did E + 1 according to the formula in the last)

## C++ Program to estimate value of e^x

#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main () { int A, B; double E, D; double X, Y; cout<<"Enter The Value of X to estimate e^X : "; cin>>X; for (A=1; A<=100; A++ ) { D=1; for (B=1; B<=A ; B++ ) { D = D * B; } Y = pow (X,A); E = E + Y / D; } cout<<setprecision(10)<<"Estimated Value of e^X is "<< E + 1 <<endl; system("pause"); }

We will use the formula **e^x = 1+x/1! + (x^2)/2! + (x^3)/3!....** to estimate value of e^x (e power x) so the C++ code used to estimate value of e will be incorporated into it.

## Logic:

Again we can achieve this by doing a few additions to our previous code. We only need to replace one which was being divided by the factorial with a variable X. We will enter this into program. Power of X can be calculated by the function pow(base,power). Where A is going to be the power which will increase by a factor of one due to the for loop.

**Line 3:** Includes the <cmath> library which contains the pow() function.

**Line 14:** Prompts for value of X.

**Line 16-22:** Code for estimating value of 'e' is incorporated here.

**Line 23:** Variable Y is assigned the value of pow(X,A) where A increases by a factor of 1 each time the loop runs.

**Line 24:** Calculation according to the given formula.

**Line 26:** Prints the estimated value of e^x after adding one to it like we did in our previous code.

This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.

**© 2014 StormsHalted**