Hi everybody, here i am again ¬¬,

I am doing an exercise that ask me to write a program to calculate this constant:

e = 1 + 1/1! + 1/2! + 1/3! ...

The user need to input how much the program must calculate.

I wrote this code, but i am not sure that the result is right, i just want to confirm that it is ok.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
   int nFactorials;
   int cNumber;
   int power = 1;
   double cFactorial;
   int counter = 1;
   double e;

   cout << "This program calculates this constant -> e = 1/1! + 1/2! + 1/3! + 1/4! + 1/5! ..." << endl;
   cout << "Type the precision as an integer: ";
   cin >> nFactorials;

   e = 1;

   while ( counter <= nFactorials )
   {
      cNumber = nFactorials - nFactorials + counter;

      cFactorial = cNumber * ( cNumber - power );

      power++;

      while ( power < cNumber )
      {

         cFactorial = cFactorial * ( cNumber - power );

         power++;

      }

      power = 1;

      if ( cFactorial > 0 )
         e += 1 / cFactorial;

      counter++;


   }

   cout << "Constant result: " << e << endl;

   return 0;

}

Here is some results:

% ./b
This program calculates this constant -> e = 1/1! + 1/2! + 1/3! + 1/4! + 1/5! ...
Type the precision as an integer: 2
Constant result: 1.5

% ./b
This program calculates this constant -> e = 1/1! + 1/2! + 1/3! + 1/4! + 1/5! ...
Type the precision as an integer: 3
Constant result: 1.66667

% ./b
This program calculates this constant -> e = 1/1! + 1/2! + 1/3! + 1/4! + 1/5! ...
Type the precision as an integer: 4
Constant result: 1.70833

Is it ok?

Thanks.

I thought that it is only a sum with the factorial calculation, ex:

e = 1 + 1/1!(0) + 1/2!(0.5) + 1/3!(0.33333)

e = 1 + 0 + 0.5 + 0.33333

e = 1.833333

I am not very good at math, but, this representation in your topic is the same constant that i am doing?

Thanks

1/1! is not 0, it's 1. Do the math -- what is 1!?

Now i think it's right:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
   int nFactorials;
   int cNumber;
   int power = 1;
   double cFactorial;
   int counter = 1;
   double e;

   cout << "This program calculates this constant -> e = 1/1! + 1/2! + 1/3! + 1/4! + 1/5! ..." << endl;
   cout << "Type the precision as an integer: ";
   cin >> nFactorials;

   e = 1;

   while ( counter <= nFactorials )
   {
      cNumber = nFactorials - nFactorials + counter;

      if ( cNumber > 1 )
         power = cNumber - 1;
      else
         power = 1;

      cFactorial = cNumber * power;

      power--;

      while ( power >= 1 )
      {

         cFactorial = cFactorial * power;

         power--;

      }

      e += 1 / cFactorial;

      counter++;


   }

   cout << "Constant result: " << e << endl;

   return 0;

}

% ./b
This program calculates this constant -> e = 1/1! + 1/2! + 1/3! + 1/4! + 1/5! ...
Type the precision as an integer: 5
Constant result: 2.71667

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.