# Help with series in C++



## hitman050 (Aug 18, 2008)

Can anyone help me with these in C++?

The program is supposed to display solution for series for a given value of n and x. There are two series

1. x - x^3/3! + x^5/5! - x^7/7! + x^9/9! ...... till x^(2n+1)/(2n+1)!

2. 1 - x + x^2/2 - x^3/3 + x^4/4 - x^5/5 ....... till x^n/n

Thanks in advance.


----------



## mehulved (Aug 18, 2008)

Aren't they limits of sin x and e^(-x)


----------



## hitman050 (Aug 18, 2008)

No idea about that. But I need to know how to display the series for n.


----------



## redhat (Aug 18, 2008)

i dont know C++, but I can give u the logic behind the code...
after input of values of x and n,
set flag=1
run a loop where the counter is initialised to 1 and incremented by 2 upto (2n+1)
if flag = 1
s=s+((X^counter)/factorial of counter)
flag = 0
else
s=s-((X^counter)/factorial of counter)
flag=1

(where factorial of counter is calculated in another part and called using messages)

2nd series:
same logic as above, but initialise counter at 0, and upto n


ican give u the complete code in JAVA if u wish, but i dont know C++


----------



## ashfame (Aug 18, 2008)

```
#include<iostream.h>
#include<conio.h>

void main()
{ clrscr();
int x,n,sum=0,flag=0;
cout<<"Enter x,n"<<endl;
cin>>x>>n;
for(int i=0;i<n;i=i+1)
{
	if(!flag)
        {
		sum=sum+(x^(2n+1))/factorial(2n+1);
                flag=1;
        }
	else
	{
             	sum=sum-(x^(2n+1))/factorial(2n+1);
                flag=0;
        }
}
cout<<"Sum is "<<sum;
getch();
}
```

also make a function factorial(int) that will calculate the factorial of argument and return it. Similarly use it to calculate the sum of 2nd series.

P.S. - I don't have any compiler with me. Understand the logic and try yourself


----------



## hitman050 (Aug 18, 2008)

I haven't been taught functions yet. Any other way around the factorial part?


----------



## karmanya (Aug 18, 2008)

You want to create 2 for loops, one nested inside the other.
Try it, should work.


----------



## ashfame (Aug 18, 2008)

hitman050 said:


> I haven't been taught functions yet. Any other way around the factorial part?



Function is used because we want to calculate factorial in every iteration. It can be done without function too. 
Just use a variable to calculate factorial before calculation sum.

like

fact= <calculate using value of i>

sum=sum+.../fact;

I hope I am clear


----------



## hitman050 (Aug 18, 2008)

I will try it.

I am not aware of logical operator '!'. What does it do?


----------



## Bandu (Aug 18, 2008)

@hitman: You might have to first go through the functions part. That comes first when learning modular languages. Will make your life easier. Ofcourse, there is a way around it for the time being.

Original code:

```
if(!flag)
	sum=sum+(x^(2n+1))/factorial(2n+1);
```

New code that does not use factorial:

```
int fact, base = 0;
if(!flag)
{
	fact = 1;
	base = 2*n + 1;
	while(base >= 2)
	{
		fact = fact * base - 1;
		base = base - 1;
	}
	sum=sum+(x^(2n+1))/fact;
}
```

do the same for the else part. And remember that your sum should be a float and not an int.

I do not have C++ compiler myself.


----------



## ashfame (Aug 18, 2008)

hitman050 said:


> I will try it.
> 
> I am not aware of logical operator '!'. What does it do?



It just inverts the operand.
0 -> 1
1 -> 0


----------



## hitman050 (Aug 18, 2008)

Can you please explain (!flag)?


----------



## ashfame (Aug 18, 2008)

Bandu said:


> And remember that your sum should be a float and not an int.



Yeps! It should be float else decimal part will be lost everytime and sum will be less than what it should be.



hitman050 said:


> Can you please explain (!flag)?



if flag=0 then !flag=1

we are just using flag to keep check on the thing that series is like, you add one term and then subtract the next term and then add the next term and so on.

I forgot to change the value of flag. Sry for that. Will edit it right away.


----------



## Bandu (Aug 18, 2008)

hitman050 said:


> Can you please explain (!flag)?


 
If your question is about operator !
! is a unary operator - meaning takes only one operand and takes a boolean operand always (atleast in Java. I forgot about C++ and I forgot whether C++ has boolean as a datatype or does it use 0 and 1)

Ex: x is a boolean variable.
x = true;

cout>>x; // Will output true
cout >> !x; // Will output false

Now, don't ask us whats _unary_ and whats _boolean_. If those are your doubts then you might want to log off and get back to Chapter 1 in your books.

If your question is about (!flag), then the author meant if(flag == false)
if(!flag) is just a short for if(flag == false)

Again, depending on the programming language, you might have to replace false with 0 and true with 1 in above snippets.


----------



## Count0paw (Oct 2, 2008)

sin x = x - x^3/3! + x^5/5! - x^7/7! + ...<------- nice topic on C++
been looking for this code but i need ..any one convert this to JAVA....
been Using Jcreator now.. just past by solving perfect number ..this my problem now 

in code this to JAVA
sin x = x - x^3/3! + x^5/5! - x^7/7! + ...


----------

