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

Viewing 1 reply thread
  • Author
    Posts
    • #2169
      SilviaJamieson
      Participant

      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 two

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

      Thanking you in advance, daisy……

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



Viewing 1 reply thread
  • The forum ‘C Programming’ is closed to new topics and replies.