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

Updated on October 28, 2016
alikhan3 profile image

The author is an undergrad electrical engineering student continually facing challenging doses of higher engineering mathematics!

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");
}
Program to find factorial of a given number.
Program to find factorial of a given number.

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)

Program to estimate the value of 'e'.
Program to estimate the value of 'e'.

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.

Program to estimate the value of e^x (e power x)
Program to estimate the value of e^x (e power x)

© 2014 StormsHalted

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      denise 2 years ago

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

    • M K Paul profile image

      M K Paul 4 years ago from India

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

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, turbofuture.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: "https://turbofuture.com/privacy-policy#gdpr"

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)