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

#3500
GWILouisaxwzkla
Participant

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 :



int i = 1;
char c = 'd';
.....

cout << "the int is: " << i << endl; //output : the int is: 1
cout << "the char is" << c << endl; // output : the char is d

for the program , 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
//#include
//#include
#include
//#include
//#include

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 ;
}