0

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 by happygeek: fixed formatting

4
Contributors
6
Replies
8
Views
9 Years
Discussion Span
Last Post by ddanbe
0

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

0

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.

0

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".

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.