Home › Forums › C Programming › C program that computes the value of ex using a formula
- This topic has 2 replies, 2 voices, and was last updated 15 years, 11 months ago by SilviaJamieson.
- AuthorPosts
- December 16, 2008 at 11:57 am #2170SilviaJamiesonParticipant
Help please with program, I seem to have blocks when it comes to anything that has an equation in it!
Write a C program that computes the value of ex by using the following formula.
e^x = 1 + x/(1!) + x^2/(2!) + x^3/(3!)+…x^n/(n!)
– The program should ask user to input value of x and number of terms (n) to approximate value of ex
– Then it calculates and displays the approximated value of the result
– Compare the approximated value of ex with the C library function exp() and find the absolute [use fabs()]difference between the two.An example of input/output dialog is shown below:-
Enter value of x : 2
Enter number of terms (n) : 18
The approximated value of ex : 7.389056098885
The value of ex using C library function : 7.389056098931
The absolute difference between the two value :
0.000000000046Many thanks, daisy…..
- December 16, 2008 at 8:10 pm #3501GWILouisaxwzklaParticipant
try:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061<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:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758<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 /> - December 17, 2008 at 1:54 pm #3502SilviaJamiesonParticipant
Thanks dman, and for the correction to the other code. I am trying to learn computer programming at home and you are making it so much easier to understand, for that i am very grateful.
daisy….
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.