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

*The author is a great enthusiast of swimming and his passion has driven him to explore many such places in the city.*

This is a simple code for the program to estimate the 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 additions to estimate the value of e^x.

## C++ Program to Find Factorial of a 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**, the first time the loop runs B, which 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.

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.

## C++ Program to Estimate the Value of 'e'

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 the Value of e^x

We will use the formula **e^x = 1+x/1! + (x^2)/2! + (x^3)/3!....** to estimate the value of e^x (e power x) so that 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**

## Comments

**denise** on April 05, 2016:

thank you so much! it helps a lot~ good job

**M K Paul** from India on January 24, 2014:

though I am a software engineer and my platform was in MFC and then core C++.Useful hub I must say..Good Job.