1. ## Calculating number e

Hello,

I am trying to calculate number "e", up to a given precision, but I face with infinite loop while using "for-loop". I couldnt understand it.

Code:
```#include <stdio.h>
#include <math.h>

int main (void) {

int acc,counter;
float sum=1 , fake;

printf("Up to which term you want to get closer to #e?\n");
scanf("%d",&acc);                 // obtain data about precision.

if(acc<2)    {                    //if accuracy is 1, e is directly 1.
printf("#e is close to 1");
}

else {                            //if accuracy is bigger than 1, run here

for(counter=1; counter<acc; counter++ ) {

fake=1;                //initialize value of "fake" every time.

while (counter>0) {  //find factorial terms

fake*=counter;
counter--;
}

sum+= (1/fake);    //add 1/factorial to sum

}

printf("you accurate term is %.2f", sum);

}

return 0;

}``` 2. You mistake is in the while loop. It always thinks it's a positive number so it decreases by 1. Then the for loop increases by one so it never gets anywhere. I don't know what fake* is so I can't comment on that. 3. Originally Posted by Tien Nguyen
I don't know what fake* is so I can't comment on that.
It is the result of arguably poor formatting, i.e., this:
Code:
`fake*=counter;`
would be somewhat more readable as:
Code:
`fake *= counter;` 4. Originally Posted by laserlight It is the result of arguably poor formatting, i.e., this:
Code:
`fake*=counter;`
would be somewhat more readable as:
Code:
`fake *= counter;`
I see. It is implying multiply from my vague memory. Thank you. Good thing I still have the user's guide and reference book to look it up if I ever need more information. 5. I do not know what you are trying to accomplish, but from looking carefully at your program there seems a slight change should do the trick. Let me know if the result is what is right. You might not get the precision you need if acc is just an integer instead of a float. I had to adjust with a type cast to float in one of the calculations.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{

int acc, counter, temp;

float sum=1, fake;

printf("Up to which term you want to get closer to #e?\n");

scanf("%d",&acc);                 /* obtain data about precision. */

if(acc<2)                         /*if accuracy is 1, e is directly 1. */
{
printf("#e is close to 1");
}
else
{                             /*if accuracy is bigger than 1, run here */
for(counter=1; counter<acc; counter++ )
{

fake=1;                       /*initialize value of "fake" every time. */
temp = counter;
while (counter>0)
{                     /*find factorial terms*/
fake*=(float)counter;
counter--;
}
counter = temp;
sum+= (1/fake);               /*add 1/factorial to sum*/
}

printf("you accurate term is %.2f", sum);
}

return(0);
}``` 6. Somewhat cleaned up example. Calculating from term to 1 is a bit more accurate as it reduces truncation. An input of 17 terms is all that is needed.

Code:
```#include <stdio.h>
#include <math.h>

int main (void) {
int term, counter, temp;
double sum = 0., fact;

printf("Up to which term you want to calculate e : ");
scanf("%d",&term);                  /* prompt for number of terms */

for(counter = term; counter > 0; counter-- ) {
fact = 1.;                      /* generate factorial */
for(temp = counter; temp > 1; temp--)
fact *= temp;
sum += (1./ fact);              /* add 1/factorial to sum */
}

printf("e is approximately %17.15lf\n", sum);

return 0;
}``` 7. Isn't e supposed to be 2.718 ... ? 8. Originally Posted by Tien Nguyen Good thing I still have the user's guide and reference book to look it up if I ever need more information.
Mathematical operators are a very basic component of the C language. It is best to commit all language features to memory, so that reference books are unnecessary. You should never need to look up a reference for what a multiplication-assignment operator does. 9. Originally Posted by whiteflags
Isn't e supposed to be 2.718 ... ?
Yeah, looks like the first term of 1 is missing, but that is easily fixed by starting sum from 1 instead of 0. 10. That should have been counter >= 0, not counter > 0. That was the last fix I made, but apparently I copied and pasted the version prior to this fix. Corrected version (with the intent to sum 1/n! + ... + 1/1! + 1/0!):

Code:
```#include <stdio.h>
#include <math.h>

int main (void) {
int term, counter, temp;
double sum = 0., fact;

printf("Up to which term you want to calculate e : ");
scanf("%d",&term);                  /* prompt for number of terms */

for(counter = term; counter >= 0; counter--) {
fact = 1.;                      /* generate factorial */
for(temp = counter; temp > 1; temp--)
fact *= temp;
sum += (1./ fact);              /* add 1/factorial to sum */
}

printf("e is approximately %17.15lf\n", sum);

return 0;
}``` 11. Thank you all for your helpful answers. They all helped  Popular pages Recent additions #include, fake;, int, number, precision 