#include<iostream>// e^x with factorial function
using namespace std;
#include<cmath>
double factorial(double);
int main ()
{
int i=1;
double x, a=1, j, y, z, E=0.0000000001;//E is error rate
cout<<"enter x to get e^x : "<<endl;
cin>> x;
y=x;
for( ; ; )
{
j=factorial(i);
z=y/j;
if( fabs(z)<E)
break;
else
{
a=a+z;
y=x*y;
i++;}
}cout<<"e ^x = "<<a<<" exact = "<<exp(X)<<" error rate "<<exp(x)-a <<endl;
return 0;
}
double factorial (double n)
{
double fact=1;
if((n==0)||(n==1))
fact=1;
else{
for(int j=2; j<=n; j++)
{
fact*=j;
}
}
return fact;
}
sniper6560 0 Newbie Poster
VernonDozier 2,218 Posting Expert Featured Poster
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.