# Error while calculating sum of n terms of a exponential series to n terms:

• 02-03-2012
abhishekcoder
Error while calculating sum of n terms of a exponential series to n terms:
Following is my program for calculating the sum of exponential series upto n terms:

e = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + 1/5!+....1/n!

Code:

```#include<stdio.h> int main() {   int n,i,p=1;   float sum=1.0,fact=1.0;   printf("enter the number of terms");   scanf("the number of terms are %d",&n);   for(i=1;i<=n;i++)   {                   while(p<=i)                   {                   fact*=p;                   p++;                   }   sum+=(1.0/fact);   }   printf("the sum is %f",sum);   system("pause");   return 0; }```
While executing the code in dev-C++, the program stops executing and goes into an infinite loop after entering 'n'. Kindly help.
• 02-03-2012
quzah
You never reset the value of p, did you mean to?

Quzah.
• 02-03-2012
abhishekcoder
It is not needed, is it? Everythime the next factorial is computed the variable fact alreaddy has the value of factorial of the earlier number. I am still getting the same problem, my program, even though it is running fine, doesn't proceed after entering the value of n.

Quote:

Originally Posted by quzah
You never reset the value of p, did you mean to?

Quzah.

• 02-03-2012
Salem
> printf("enter the number of terms");
> scanf("the number of terms are %d",&n);
Perhaps you should just have
scanf("%d",&n);

Otherwise, you have to type in
the number of terms are 7
• 02-03-2012
camel-man
Quote:

Otherwise, you have to type in
the number of terms are 7
Salem, would that even be legal thing to do in C?
• 02-03-2012
Salem
> Salem, would that even be legal thing to do in C?
Try it :)

I mean of course, RTFM
Quote:

A directive is one of the following:

· A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space, including
none, in the input.

· An ordinary character (i.e., one other than white space or '%'). This character must exactly match the next character of input.

· A conversion specification, which commences with a '%' (percent) character. A sequence of characters from the input is converted according to
this specification, and the result is placed in the corresponding pointer argument. If the next item of input does not match the conversion
specification, the conversion fails — this is a matching failure.
• 02-03-2012
abhishekcoder
Thank youu so much guys! Couldn't spot the obvious :)
• 02-03-2012
iMalc
Calculate the factorial iteratively; you only need one loop.
If the last factorial calculated was 4! then just multiply that previous value by 5 and behold you now have 5!
• 02-04-2012
Stevan
Code:

`float sum=1.0,fact=1.0;`
Your sum should be equal to 0 not 1, cause you are not multiplying sum, you are adding.
• 02-04-2012
abhishekcoder
Thanks everyone. @Stevan - actually the first term of my series is 1 which is not calculated while coding, so I am writing sum as 1 initially.

Quote:

Originally Posted by Stevan
Code:

`float sum=1.0,fact=1.0;`
Your sum should be equal to 0 not 1, cause you are not multiplying sum, you are adding.

• 02-04-2012
Stevan
@abhishekcoder, which term, 1/0! ?
• 02-04-2012
abhishekcoder
Yes.