#WSQ12

This is a video that will help you calculate euler’s number, which is an irrational number… I’m having a little trouble with the function because it’s totally ignoring the precision (which is the number that the user gives, but it does calculate euler’s number correctly.

Here is the code:

#include <iostream>
#include <cmath>
using namespace std;

float factorial(int n)
{
if(n==0)
{
return 1;
}
else
{
return n*factorial(n-1);
}
}

float calculate_e(float precision)
{
float e=1;
float previous;
int i=1;

do
{
previous=e;
e=e+1/(factorial(i));
cout<<e<<endl;
i++;
} while(abs (e-previous)>precision);
return e;
}

int main()
{
float precision;
cout<<“How many decimals of accuracy do you want? “;
cin>>precision;
cout<<“The estimation of e with “<<precision<<” decimal points is: “<<calculate_e(precision)<<endl;
return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s