Home › Forums › C Programming › C program that estimates the value of the math constant e

- This topic has 1 reply, 2 voices, and was last updated 12 years, 10 months ago by GWILouisaxwzkla.

- AuthorPosts
- December 15, 2008 at 3:19 pm #2169SilviaJamiesonParticipant
Hi everyone, wonder if anyone can point me in the right direction for this problem I have looked at tutorials but I don’t understand why some have #include

while others have #include and also I have never seen cin>> and < Write a C program that estimates the value of the mathematical constant e by using the

following formula.e= 1 + 1/(1!) + 1/(2!) + 1/(3!) +…1/(n!)

– The program should ask user to input the number of terms (n) to approximate value of e

– Then it calculates and displays the approximated value of constant e

– Compare the approximated value of e with the given mathematical constant

e = 2.7182818284590… and find the absolute [use fabs()] difference between the twoAn example of input/output dialog is shown below:-

Enter number of terms (n) : 15

The approximated value of e : 2.718281828458

The given mathematical constant e = 2.718281828459

The absolute difference between the two value : 0.000000000001Thanking you in advance, daisy……

- December 15, 2008 at 8:34 pm #3500GWILouisaxwzklaParticipant
cout and cin are input and output members of a stream object in the file iostream.h . This header file is associated with c++ programs and the method that most c++ programmers use for input and output. cout stands for “console output” and cin stands for console input. You can use them more easily than printf and scanf :

12345678910<br /><br />int i = 1;<br />char c = 'd';<br />.....<br /><br />cout << "the int is: " << i << endl; //output : the int is: 1<br />cout << "the char is" << c << endl; // output : the char is d<br /><br />for the program , try:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960<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 <br />//#include <br />//#include <br />#include <br />//#include <br />//#include <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 />return 0 ;<br />}<br /><br /><br /><br />

- AuthorPosts

- The forum ‘C Programming’ is closed to new topics and replies.