#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    // Declare constants and variables
    const double ACCURACY = 0.00001;
    double x, factorial, ex_approx, ex_funct, curr_term;
    int terms, i;
    // Read value of x for exp(x) calculation
    cout << "Enter the value of x you wish to use for exp(x) approximation\n";
    cin >> x;
    // Calculate exp(x) using library function
    ex_funct = exp(x);
    // Initialization
    ex_approx = 1;          // first term in approximation
    terms = 1;
    // Approximate exp(x) term by term
    while((ex_funct - ex_approx) > ACCURACY)
    {
        // Calculate terms!
        factorial = 1;
        for (i=1; i<= terms; i++)
            factorial = factorial * i;
        // Calculate new term
        curr_term = pow(terms, x)/factorial;

        // Update number of terms used
        terms++;
    }

        cout << "x = " << x << " the approximation = " << ex_approx << " exp(x) = " << ex_funct
         << "the number of terms is " << terms + 1<< endl;

}

I cant figure out what I am dong wrong

sorry

Recommended Answers

All 2 Replies

Member Avatar for iamthwee

Start throwing a few couts there to see where you're going wrong. Do any of your variables get updated in the body of the while loop?

Might it be a good idea to create a separate factorial function for clarity etc?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.