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 13 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:

/****************************************************************

* File Name : c:programstempCG.cpp

* Date : December,15,2008

* Comments : new project

* Compiler/Assembler :

* Modifications :

*

*

*

*

*

* Program Shell Generated At: 3:08:27 p.m.

=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#include < iostream >

//#include < string.h >

//#include < conio.h >

#include < math.h >

//#include < iomanip >

//#include < ctype.h >

using namespace std;

//main function ******************************

int main ( )

{

int terms;

double powerOfX;

double e = 1;

double i = 1;

double j ;

double k = 2;

printf ( "Enter value of x : " );

scanf ( "%lf" , & powerOfX );

printf ( "Enter the number of terms ( greater or equal to one ) : " );

scanf ( "%i" , & terms );

j = powerOfX;

while ( terms > 1 )

{

e = e + ( j * ( 1 / i ) );

terms --;

i = i * k;

j = j * powerOfX;

k ++;

}

printf ( "The approximated value of ex : %.12lf n" , e );

printf ( "The value of ex using C library function %.12lf: n" ,exp( powerOfX ) );

printf ( "The absolute difference between the two value : " );

printf ( "%.12lf n" , fabs ( exp ( powerOfX ) - e ) );

return 0 ;

}

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:

/****************************************************************

* File Name : c:programstempCG.cpp

* Date : December,15,2008

* Comments : new project

* Compiler/Assembler :

* Modifications :

*

*

*

*

*

* Program Shell Generated At: 3:08:27 p.m.

=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#include < iostream >

//#include < string.h >

//#include < conio.h >

#include < math.h >

//#include < iomanip >

//#include < ctype.h >

using namespace std;

//main function ******************************

int main ( )

{

int terms;

double powerOfX;

double e = 1;

double i = 1;

double j ;

double k = 2;

printf ( "Enter the number of terms ( greater or equal to one ) : " );

scanf ( "%i" , & terms );

while ( terms > 1 )

{

e = e + ( 1 / i ) ;

terms --;

i = i * k;

k ++;

}

printf ( "The approximated value of e : %.12lf n" , e );

printf ( "The given mathematical constant e = 2.718281828459 n");

printf ( "The absolute difference between the two value : " );

printf ( "%.12lf n" , fabs ( 2.718281828459 - e ) );

return 0 ;

}

- 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.