What is the code for ( e^x = 1 + x/1! + x^2/2! + x^3/3! + ..... )

anyway can help me, plzzz :cry: :rolleyes:

Dani AI

Generated

A concise, robust way to compute e^x from the series is to generate each term from the previous one instead of recomputing factorials or calling pow. correctly flagged that ^ is bitwise XOR and not exponentiation; ’s idea of tracking power and factorial separately works, but computing x^n and n! independently wastes work and can overflow integer factorials quickly. ’s push to use a loop is right—use a single loop that updates a running term.

Algorithm (simple and stable):

  • Start with term = 1 (this is x^0/0!) and sum = 1.
  • For i = 1..maxIter do term *= x / i (so term becomes x^i / i!), then sum += term.
  • Stop early when the absolute value of term is below a chosen tolerance eps. This avoids explicit factorials and pow, keeps only one multiply and one divide per iteration, and is numerically cheaper.

Example implementation:

#include <iostream>
#include <iomanip>

int main() {
    long double x = 2.0L;            // input value (use long double for more precision)
    const int maxIter = 200;
    const long double eps = 1e-18L;

    long double term = 1.0L;         // x^0 / 0!
    long double sum = term;

    for (int i = 1; i <= maxIter; ++i) {
        term *= x / i;               // recurrence: term_i = term_{i-1} * x / i
        sum += term;
        long double abs_term = term < 0 ? -term : term;
        if (abs_term < eps) break;
    }

    std::cout << std::setprecision(18) << sum << '\n';
    return 0;
}

Notes and cautions:

  • Use long double for better precision; a smaller eps yields more accuracy but more iterations. For double use eps ≈ 1e-15.
  • For large |x| the series needs many terms; if performance or huge ranges are required, either use std::exp or split the exponent (e.g., compute e^(x/2^k) and square repeatedly).
  • Never store factorials in plain int (they overflow fast). The recurrence avoids that entirely.

Recommended Answers

All 8 Replies

Use a 'for' loop. Show what you think works, and tell us exactly what each line does and why.

In doing this, you'll probably solve your problem, but if that fails, you'll have people here willing to help you.

thanks for replyiing me,

But i wanna write the code for (e^x = 1 + x/1! + x^2/2! + x^3/3! + ..... ) without using math library.

#include<iostream.H>

int main()
{

//(e^x = 1 + x/1! + x^2/2! + x^3/3! + .....)

int result = 1;
int x = 2;
int z = 1;
int e = 1;

for (int i = 1 ; i < 4 ; i++)
{
    // This is the factorial part & its not working well, i need to fix tht, plz help (i dont wanna use any function)
    while ( z != 0)
    {
    result *= z;
    z--;
    }

    //cout << "Result = " << result << endl;
    //cout << "Z = " << z << endl;

    e = e + x^i/result;

    z++;
}

cout << "The Sum = " << e << endl;

return 0;
}

I think you know how to take a factorial of a number, but your variable usage is not good.

while ( z != 0)
{
result *= z;
z--;
}

Now, honestly, that works if you first set z to the value of whatever you want to take the factorial of, and set result to one. You're setting z equal to one before the for loop, and then incrementing it at the end of each loop, but you decrement it to zero before that, so your while loop is *entirely pointless*.

Just multiply result by i, you don't need a while/for loop or whatever.

-Fredric

#include<iostream.H>

int main()
{

//(e^x = 1 + x/1! + x^2/2! + x^3/3! + .....)

int result = 1;
int x = 2;
int e = 1;

for (int i = 1 ; i < 4 ; i++)
{

	result *= i;
	
	//cout << "Result = " << result << endl;
	
	e = e + x^i/result;

}

cout << "The Sum = " << e << endl;

return 0;
}

<< moderator edit: added [code][/code] tags >>


You mean like this??? :?:

I didn't see it the first time, but the ^ doesn't do what you think it does in C++, if you want to take m to the n power then you use the pow function like so...

#include <math.h>

...

double m=2.0, n=3.0, result;
result = pow(m, n);

That would return 2 to the 3rd, or 8. Hope that helps a bit.

-Fredric

Hi, I have some issues with your variable names. result is actually not the result, and e is actually the natural log constant in your comments. Made some changes to the var and trying to do away with the pow fn.
Not sure if you want to restrict your input to just integral values, but output will most definitely be double?

int main()
{

  //(e^x = 1 + x/1! + x^2/2! + x^3/3! + .....)

  double x = 2.0; // input value

  int factorial = 1;
  double pow_x = 1.0;
  double result = 1.0;

  cout << "e^" << x << " = 1";

  for (int i = 1 ; i < 4 ; i++)
  {

	factorial *= i;
	pow_x *= x;
	
	cout << " + " << pow_x << "/" << factorial;
	
	result = result + pow_x/factorial;

  }

  cout << endl << "= " << result << endl;

  return 0;
}

I've not tried the code, but should be ok ...

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.