I need help on writing a C++ program to compute Euler's number it being

e= 1/0! + 1/1! + 1/2! + ... + 1/n!

Please help I am new at the programming scene I'd greatly appreciate any feedback

Printable View

- 06-09-2007BJtoVisualcC++Euler's Number
I need help on writing a C++ program to compute Euler's number it being

e= 1/0! + 1/1! + 1/2! + ... + 1/n!

Please help I am new at the programming scene I'd greatly appreciate any feedback - 06-09-2007Desolation
Seems like homework to me. Tell us what you've done, what you think you can do, pseudo-code, etc.

- 06-10-2007Junior89
Its not particularly difficult, you need two functions: 1 - something to compute a factorial (simple), and 2 - something to compute the number. I wrote a program that should be working, but for some reason my computer's rounding everything to ints before it adds them =(

I'm a bit confused why a float var type would be becoming an int ?

Good luck! - 06-10-2007CodeMonkeyCode:
`return std::exp(1);`

- 06-10-2007Thantos
No real need to write a factorial function. In fact doing so would probably slow it down.

When doing this problem the first thing to realize is that n will always be greater then or equal to 0. As such 1/0! will always be done. So it is a good idea to initialize based off of that.

So we create a double to store e and give it the value of 1.0 and create an unsigned int to store the denominator and initialize it to 1 (0! is defined to 1 for this problem).

Walk though the steps for n=3:

n=0: e=1.0 d=1

n=1:

d = d * 1 (1! is 1 * 0!)

e = e + 1.0 / d

n=2:

d = d * 2 (2! is 2 * 1!)

e = e + 1.0 / d

n=3:

d = d * 3 (3! is 3 * 2!)

e = e + 1.0 / d

The higher the value of n the more precision you gain.

I hope you can see now the loop in this problem. - 06-11-2007BJtoVisualcC++
So now I just put in a while for some n value?

- 06-11-2007brewbuck
- 06-11-2007Thantos
- 06-11-2007BJtoVisualcC++what I have
see attachment

- 06-11-2007dwks
I can't open attachments on this computer, and I'm sure some people don't feel like clicking on the link, either. Since it's only 621 bytes you might as well just post it in [code] tags.

- 06-11-2007BJtoVisualcC++
- 06-11-2007Thantos
- 06-11-2007Thantos
After looking at your code I can see you totally ignored my explanation of the problem.

Let me ask these two questions:

What is the value of e?

Can a variable of type long store the value of e? - 06-11-2007Salem
> return factX;

> cout << "The Euler's number is " << factX << endl;

Remove the return, it is stopping you printing. - 06-11-2007CodeMonkeyCode:
`#include <iostream>`

#include <iomanip>

#include <string>

#include <cmath>

using namespace std;**//I would avoid this. If your teacher disagrees, strangle him.**

int main( )

{

int n; //Initializing counter**//fine, but for clarity you might want to save it for later**

long factX = 1; //setting factorial to start at 1**//e is not a type of integer! Use double.**

n = 0; //setting counter to zero**//might as well do this when you declare the variable, might as well within the loop**

for(n = 1, factX =1; n<= 10; n++) factX = factX * n;

return factX; //return to the caller? Uh oh...

cout << "The Euler's number is... doesn't matter because this never executes... " << endl;

system("pause");

return 0;

}

1. Allocate space for an integer. Call it "n".

2. Allocate space for a**long integer**. Call it factX. Initialize its value to the integral constant, 1.

3. Set n to the integral constant, 0.

4. Set n to 1. Set factX to 1. Until n is not less than or equal to 10, execute the below code, each time checking first to see if n is still less than or equal to 10. Increase n by 1 each time, after the below code is executed.

4a (The "below" code): set the value of factX so that it is equal to the product of itself and n (this will be an**integer**).

5. Now that the loop is done, get ready to return to the system process that called this program and give it the value we were calling factX. Clean up. End execution.

Is this what you want to program to do? Write out in prose what you want done, so that a computer might understand it. Then make code.