| | |
Help with series
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
Solved Threads: 0
Hello,I need help with the following series (sorry I forgot what its called..)
1/1! - 2/2! + 3/3! -4/4!....upto n terms
Here's my program :
So now my problem is,whatever input I give the answer is always 0.00
PS:Realized that it gives correct answer till 2,but after that whatever the input it gives 0.00 as the answer
1/1! - 2/2! + 3/3! -4/4!....upto n terms
Here's my program :
C Syntax (Toggle Plain Text)
#include<conio.h> #include<stdio.h> #include<float.h> void main() { float com=0,j=0,j1=0,ans=0; //variable decl of float type int tillw,i,no=0,fac1=1,fac=1,f1,f2; //variable decl of int type clrscr(); printf("\n\n\t\t *********************************"); printf("\n\t\t ---------------------------------"); printf("\n\n\t\t SERIES"); printf("\n\n\t\t ---------------------------------"); printf("\n\t\t *********************************"); printf("\n\n\n\n\t\t Enter till where you want : "); scanf("%d",&tillw); flushall(); //accept no. from user for(i=1;i<=tillw;i++) { no=no+1; ans=no%2; if(ans!=0) { //addition part of series for(f1=1;f1<=no;f1++) { fac=fac*f1; } //find factorial j=no/fac; com=com+j; } else { //subtraction part of series for(f2=1;f2<=no;f2++) { fac1=fac1*f2; } //find factorial j1=no/fac1; com=com-j1; } //printf("\n\n\t\t The answer is : %0.2f",com);(for testing answer in each step) } //main for-loop ends.. printf("\n\n\t\t The answer is : %f",com); //prints final answer getch(); } //void-main ending..
So now my problem is,whatever input I give the answer is always 0.00
PS:Realized that it gives correct answer till 2,but after that whatever the input it gives 0.00 as the answer
Last edited by bhagyaraj; Jun 1st, 2009 at 1:07 pm.
Aaaaaaaaaaargh!
Use
You should also read this.
A comment like this:
Note:: This code looks more like C code, it's definitely no C++ !!
void main() , don't use it, it's evil (check this page) !!Use
int main() instead 
You should also read this.
A comment like this:
//variable decl of float type 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++ !!
Last edited by tux4life; Jun 1st, 2009 at 1:32 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
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:
3. You will get (unsignalled) integer overflow in factorial calculation: 13! is greater than max integer value.
Better search DaniWeb for series: there are lots of threads on this topic. It's a bad approach to calculate factorial for every series member...
1. Use double type for math calculations (not float).
2. Use floating-point division if you want to get floating-point result, for example:
C Syntax (Toggle Plain Text)
com += (double)n/fac;
Better search DaniWeb for series: there are lots of threads on this topic. It's a bad approach to calculate factorial for every series member...
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
Solved Threads: 0
I changed void main to int main, also using double for calculations..
Still the same error
ArkM,what exactly you mean by saying tht it is bad to calculate factorial for each no of series.I did not understand
And also can you plz explain
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...
I could not understand understand..sorry if i am becoming a headache..
soryy for the double post,how to delete my post btw?
Still the same error

ArkM,what exactly you mean by saying tht it is bad to calculate factorial for each no of series.I did not understand
And also can you plz explain
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...
I could not understand understand..sorry if i am becoming a headache..
soryy for the double post,how to delete my post btw?
Last edited by bhagyaraj; Jun 1st, 2009 at 2:54 pm.
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
Solved Threads: 0
I changed void main to int main, also using double for calculations..
Still the same error
ArkM,what exactly you mean by saying that it is bad to calculate factorial for each no. of series.I did not understand
And also can you plz explain ...
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...
I could not understand...sorry if i am becoming too much of a nuisance .
Still the same error

ArkM,what exactly you mean by saying that it is bad to calculate factorial for each no. of series.I did not understand
And also can you plz explain ...
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...
I could not understand...sorry if i am becoming too much of a nuisance .
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.
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.
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
#include<conio.h> #include<stdio.h> #include<float.h> int main() { double com=0,j=0,j1=0,ans=0; int no=0; double tillw,i,fac1=1,fac=1,f1,f2; clrscr(); printf("\n\n\t\t *********************************"); printf("\n\t\t ---------------------------------"); printf("\n\n\t\t SERIES"); printf("\n\n\t\t ---------------------------------"); printf("\n\t\t *********************************"); printf("\n\n\n\n\t\t Enter till where you want : "); scanf("%d",&tillw); flushall(); //accept no. from user for(i=1;i<=tillw;i++) { no=no+1; ans=no%2; if(ans!=0) { //addition part of series fac=fac*i; j=(double)no/fac; com=(double)com+j; } else { //subtraction part of series fac=fac*i; j1=(double)no/fac1; com=(double)com-j1; } //printf("\n\n\t\t The answer is : %0.2f",com);(for testing answer in each step) } //main for-loop ends.. printf("\n\n\t\t The answer is : %f",com); //prints final answer getch(); return (0); } //void-main ending..
I removed the for loops for calculating the factorials,used double and removed void main.STill the same problem

Oh i dont need to go to such high numbers.I will be happy if it can calculate upto 5
Last edited by bhagyaraj; Jun 1st, 2009 at 3:21 pm.
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
The series breaks down to::
1 - 2 / (2 *1) +3 / (3*2!) -....
i.e 1 - 1 + 1/2! - 1/3!
So the first (major) 2 terms of the series cancel out.
Now, may be you define a variable
1 - 2 / (2 *1) +3 / (3*2!) -....
i.e 1 - 1 + 1/2! - 1/3!
So the first (major) 2 terms of the series cancel out.
Now, may be you define a variable
C Syntax (Toggle Plain Text)
int sign_carrier = 1 ; long fact = 1; double sum = 0; for( i = 2;i<=tillw;i++) /*start from 2*/ /*start from 3rd term in the series*/ { if (i is even) sign_carrier =1; else sign_carrier =-1; now get the factorial with the help of i; sum = sum + (sign_carrier / fact); }
Well, let f(i) is i-th member without sign (it's not C).
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!
The answer: 0.367879441171442
It's equal to 1/e constant. Exactly.
C Syntax (Toggle Plain Text)
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)
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!
c Syntax (Toggle Plain Text)
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; }
It's equal to 1/e constant. Exactly.
Last edited by ArkM; Jun 1st, 2009 at 6:20 pm.
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
#include<conio.h> #include<stdio.h> int main() { int sign_carrier,i,tillw,ans; long int fac=1; float sum=0; clrscr(); printf("\n\n\t\t *********************************"); printf("\n\t\t ---------------------------------"); printf("\n\n\t\t SERIES"); printf("\n\n\t\t ---------------------------------"); printf("\n\t\t *********************************"); printf("\n\n\n\n\t\t Enter till where you want : "); scanf("%d",&tillw); flushall(); //accept no. from user for(i=1;i<=tillw;i++) { ans=i%2; //to find odd or even.. if(ans!=0) //odd { sign_carrier=1; } else //even { sign_carrier=-1; } fac=fac*i; sum+=(double)(i*sign_carrier)/fac; } //main for-loop ends.. printf("\n\n\t\t Answer is : %0.20f",sum); getch(); return sum; } //void-main ending..
This works

Thank you all for your time.really appreciate it..

I have started it from 1 instead of 2 which you guys had suggested
Last edited by bhagyaraj; Jun 2nd, 2009 at 3:52 am.
![]() |
Similar Threads
- HP 1900 Series (Cellphones, PDAs and Handheld Devices)
- ATI Radeon x series question? (Motherboards, CPUs and RAM)
- Sony PCV-RX series power supplies (Troubleshooting Dead Machines)
- series of 1 + 1/2 + 1/3 ...etc (C++)
- sum of series (C++)
- Rename a Series of Files (Windows tips 'n' tweaks)
- HP Laserjet 1000 Series (USB Devices and other Peripherals)
Other Threads in the C Forum
- Previous Thread: Simple program understanding pointers ??
- Next Thread: Tutorials for hash table
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking highest homework i/o inches include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft mqqueue mysql number odf open openwebfoundation owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming socketprogramming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






