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

This is a discussion on Error while calculating sum of n terms of a exponential series to n terms: within the C Programming forums, part of the General Programming Boards category; Following is my program for calculating the sum of exponential series upto n terms: e = 1/0! + 1/1! + ...

1. ## 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.

2. You never reset the value of p, did you mean to?

Quzah.

3. 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.

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

Quzah.

4. > 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

5. Otherwise, you have to type in
the number of terms are 7
Salem, would that even be legal thing to do in C?

6. > Salem, would that even be legal thing to do in C?
Try it

I mean of course, RTFM
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.

7. Thank youu so much guys! Couldn't spot the obvious

8. 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!

9. 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.

10. 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.

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.

11. @abhishekcoder, which term, 1/0! ?

12. Yes.