Home › Forums › C Programming › C program that computes the value of ex using a formula › Re: Re: C program that computes the value of ex using a formula
December 16, 2008 at 8:10 pm
#3501
GWILouisaxwzkla
Participant
try:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | <br /> /****************************************************************<br /> * File Name : c:programstempCG.cpp<br /> * Date : December,15,2008<br /> * Comments : new project<br /> * Compiler/Assembler :<br /> * Modifications :<br /> *<br /> *<br /> *<br /> *<br /> *<br /> * Program Shell Generated At: 3:08:27 p.m.<br /> =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /> <br /> <br /> #include < iostream ><br /> //#include < string.h ><br /> //#include < conio.h ><br /> #include < math.h ><br /> //#include < iomanip ><br /> //#include < ctype.h ><br /> <br /> using namespace std;<br /> <br /> //main function ******************************<br /> <br /> int main ( )<br /> {<br /> <br /> int terms;<br /> double powerOfX;<br /> double e = 1;<br /> double i = 1;<br /> double j ;<br /> double k = 2;<br /> <br /> printf ( "Enter value of x : " );<br /> scanf ( "%lf" , & powerOfX );<br /> printf ( "Enter the number of terms ( greater or equal to one ) : " );<br /> scanf ( "%i" , & terms );<br /> j = powerOfX;<br /> <br /> <br /> <br /> while ( terms > 1 )<br /> {<br /> e = e + ( j * ( 1 / i ) );<br /> terms --;<br /> i = i * k;<br /> j = j * powerOfX;<br /> k ++;<br /> <br /> }<br /> printf ( "The approximated value of ex : %.12lf n" , e );<br /> printf ( "The value of ex using C library function %.12lf: n" ,exp( powerOfX ) );<br /> printf ( "The absolute difference between the two value : " );<br /> printf ( "%.12lf n" , fabs ( exp ( powerOfX ) - e ) );<br /> return 0 ;<br /> }<br /> |
I didn’t know how many digit to output so I chose 12 like the example. You can change this by changing the number listed after the ‘.’ in the last printf() statements.
Theres an error in the code I posted for the approximation of e = 1 + 1/1! + 1/2! . Heres the proper code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | <br /> <br /> /****************************************************************<br /> * File Name : c:programstempCG.cpp<br /> * Date : December,15,2008<br /> * Comments : new project<br /> * Compiler/Assembler :<br /> * Modifications :<br /> *<br /> *<br /> *<br /> *<br /> *<br /> * Program Shell Generated At: 3:08:27 p.m.<br /> =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /> <br /> <br /> #include < iostream ><br /> //#include < string.h ><br /> //#include < conio.h ><br /> #include < math.h ><br /> //#include < iomanip ><br /> //#include < ctype.h ><br /> <br /> using namespace std;<br /> <br /> //main function ******************************<br /> <br /> int main ( )<br /> {<br /> <br /> int terms;<br /> double powerOfX;<br /> double e = 1;<br /> double i = 1;<br /> double j ;<br /> double k = 2;<br /> <br /> <br /> printf ( "Enter the number of terms ( greater or equal to one ) : " );<br /> scanf ( "%i" , & terms );<br /> <br /> while ( terms > 1 )<br /> {<br /> e = e + ( 1 / i ) ;<br /> terms --;<br /> i = i * k;<br /> k ++;<br /> <br /> }<br /> printf ( "The approximated value of e : %.12lf n" , e );<br /> printf ( "The given mathematical constant e = 2.718281828459 n");<br /> printf ( "The absolute difference between the two value : " );<br /> printf ( "%.12lf n" , fabs ( 2.718281828459 - e ) );<br /> <br /> return 0 ;<br /> }<br /> |