C Programming (Turbo / Visual C++ Compiler) - Using Exponent Series and Eulers Number to find EXP Value
I have given here a C program to find the value of EXP using Exponent Series and without using the library function. I have used Eulers Number to find out the value of Exponent.
This program will print the values got from the series function MyExp1 and Eulers Number MyExp2 and library function sin.
exp(x) series = 1 + x + x^2 / 2! + x^3 / 3! + x^4 / 4! + x^5 / %! + .....
exp(x) series = power(2.71828, x)
Source Code
#include <math.h>
#include <stdio.h>
const double PI = 3.14159265;
const double EulersNumber = 2.71828;
// exp(x) series = 1 + x + x^2 / 2! + x^3 / 3! + x^4 / 4!
double MyExp1(double x)
{
double f = x;
double result = 1 + x;
double fact = 1;
int i = 0;
for(i = 2; i < 20; i++)
{
fact *= i;
f *= x;
result += f / fact;
}
return result;
}
// exp(x) series = power(2.71828, x)
double MyExp2(double x)
{
return pow(EulersNumber, x);
}
int main()
{
double i;
for( i = -2; i <= 3; i += 0.1)
{
printf("%.4lf = %.4lf %.4lf %.4lf\n",
i, exp(i), MyExp1(i), MyExp2(i) );
}
return 0;
}
Output
-2.0000 = 0.1353 0.1353 0.1353
-1.9000 = 0.1496 0.1496 0.1496
-1.8000 = 0.1653 0.1653 0.1653
-1.7000 = 0.1827 0.1827 0.1827
-1.6000 = 0.2019 0.2019 0.2019
-1.5000 = 0.2231 0.2231 0.2231
-1.4000 = 0.2466 0.2466 0.2466
-1.3000 = 0.2725 0.2725 0.2725
-1.2000 = 0.3012 0.3012 0.3012
-1.1000 = 0.3329 0.3329 0.3329
-1.0000 = 0.3679 0.3679 0.3679
-0.9000 = 0.4066 0.4066 0.4066
-0.8000 = 0.4493 0.4493 0.4493
-0.7000 = 0.4966 0.4966 0.4966
-0.6000 = 0.5488 0.5488 0.5488
-0.5000 = 0.6065 0.6065 0.6065
-0.4000 = 0.6703 0.6703 0.6703
-0.3000 = 0.7408 0.7408 0.7408
-0.2000 = 0.8187 0.8187 0.8187
-0.1000 = 0.9048 0.9048 0.9048
0.0000 = 1.0000 1.0000 1.0000
0.1000 = 1.1052 1.1052 1.1052
0.2000 = 1.2214 1.2214 1.2214
0.3000 = 1.3499 1.3499 1.3499
0.4000 = 1.4918 1.4918 1.4918
0.5000 = 1.6487 1.6487 1.6487
0.6000 = 1.8221 1.8221 1.8221
0.7000 = 2.0138 2.0138 2.0138
0.8000 = 2.2255 2.2255 2.2255
0.9000 = 2.4596 2.4596 2.4596
1.0000 = 2.7183 2.7183 2.7183
1.1000 = 3.0042 3.0042 3.0042
1.2000 = 3.3201 3.3201 3.3201
1.3000 = 3.6693 3.6693 3.6693
1.4000 = 4.0552 4.0552 4.0552
1.5000 = 4.4817 4.4817 4.4817
1.6000 = 4.9530 4.9530 4.9530
1.7000 = 5.4739 5.4739 5.4739
1.8000 = 6.0496 6.0496 6.0496
1.9000 = 6.6859 6.6859 6.6859
2.0000 = 7.3891 7.3891 7.3890
2.1000 = 8.1662 8.1662 8.1662
2.2000 = 9.0250 9.0250 9.0250
2.3000 = 9.9742 9.9742 9.9742
2.4000 = 11.0232 11.0232 11.0232
2.5000 = 12.1825 12.1825 12.1825
2.6000 = 13.4637 13.4637 13.4637
2.7000 = 14.8797 14.8797 14.8797
2.8000 = 16.4446 16.4446 16.4446
2.9000 = 18.1741 18.1741 18.1741
Press any key to continue . . .
|