Here is the Quesion ::
I want to write a program that estimates the value of the mathematical constant e by using the formula:

e = 1 + 1/1! + 2/2! + 3/3! + 4/4! + ………+ n/n!

Here is my Answer:

#include <iostream.h>
int main ()
{
double x , f=1 , n ,i ,r ,s=0;
cout<<"Enter a number";
cin>>n;
x=1;
while (x<=n)
{
f=f*x;


i=0 ;
while (i<=x)
{
r=n/f;


i++;
}
s=s+r;
x++;
}
cout<<"e="<<s;
return 0;
}

I'm not sure if its correct ?? can anyone help me ? and i want to ask why do i get the same value when i enter a number >5??

Edited 3 Years Ago by happygeek: fixed formatting

ok sorry i will use it next time !!
but you didnt answer my Question !!

What has doing the constant e to do with your algorithm for calculating it?
Did you already do factorials?
It is always a good thing to split up a problem in a smaller one. So I should first make a function to calculate factorials and use that function to calculate e.
Happy programming.

SQ89,
are you sure your formula to calculate e is correct?.
Google "Derangements" and you should be able to find the correct formula for it. Alternatively you can look for "hat check problem".

freelancelote you are right, the formula is not correct.
SQ89 you must be able to find thatone for yourself!

This article has been dead for over six months. Start a new discussion instead.