Aaaaaaaaaaargh! <strong>void main()</strong> , don't use it,it's evil (check this page) !!
Use int main() instead :)
You should also read this .
A comment like this: <strong>//variable decl of float type</strong> makes no sense, everyone can see that the declared variables are of type float.Note:: This code looks more like C code, it's definitely no C++ !!
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
See: you have fac > no for all no values because fac = n!. Therefore no/fac == 0 (integer division) and com=com+j is the same as com += 0...
1. Use double type for math calculations (not float).
2. Use floating-point division if you want to get floating-point result, for example:
com += (double)n/fac;
3. You will get (unsignalled) integer overflow in factorial calculation: 13! is greater than max integer value.
Better search DaniWeb forseries: there are lots of threads on this topic. It's a bad approach to calculate factorial for every series member...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
you dont need to recalculate each and every factorial. because if you've already calculated 9! (for example), then 10! is really just 9! * 10.
and basically it's this: you can't do integer division. because all your quotients will be integer zeros.
3/6 = 0. 4/24 = 0. 5/120 = 0.
you have to use the "double" data type.
and as he already mentioned, it will crap out at 13!, because the maximum unsigned integer 2^32 = ~4 billion.
look into "long long int" data types. and "big number" libraries if you really need to get deep.
power series are not a trival matter in C.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
Well, let f(i) is i-th member without sign (it's not C).
f(i) = i/i! = i/(1*2*...*(i-1)*i) =
i/(i*(1*2...*(i-1)) = 1/(i-1)!
f(i+1) = 1/i! = 1/((i-1)!*i) = f(i)/i
i = 1, 2,... (0! = 1 by definition)
In other words, if we know i-th member of series then the next member is equal to this member / i (ignore sign).
if we know that i-th member is positive then the next member is negative and vice versa.
We know that 2-nd member is equal to -1.0 (negative). We know that the partial sum of the series is equal to 1.0 at this moment: f(1) == 1/1!...
Now we must define calculation loop condition. Obviously no sense to continue calculations when the next member of series is too small: double type precision is ~16 decimal digits. We want to stop calculations when f(i) < eps, where eps == 1e-16 (for example).
Yet another tip: no need in int variables at all!
double Series()
{
const double eps = 1e-16;
double sum = 1.0; /* sum(1) */
double f, i;
int neg = 1;/* the 2nd member < 0 */
for (f = 1.0, i = 2.0; f > eps; f /= i, i += 1.0) {
/* cout << i << '\t' << f << '\t'; */
sum += (neg? -f: f);
/* cout << setprecision(15) << sum << '\t'
<< (sum?1/sum:0) << '\n'; */
neg ^= 1;
}
/* cout << sum << endl; */
return sum;
}
The answer: 0.367879441171442
It's equal to 1/e constant. Exactly.
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348