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 :

#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

Aaaaaaaaaaargh! [B]void main()[/B] , don't use it, it's evil (check this page) !!
Use int main() instead :)

You should also read this.

A comment like this: [B]//variable decl of float type[/B] 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++ !!

commented: Yes! +32

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 for series: there are lots of threads on this topic. It's a bad approach to calculate factorial for every series member...

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?

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 .

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.

#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:)

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

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

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.

#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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.