DIGIT-ARC read-only archive · snapshot: July 23, 2026

Help with series in C++

15 posts · originally from https://geek.digit.in/community/threads/help-with-series-in-c.95894/

H
hitman050 Original post #1
Aug 18, 2008 at 5:02 PM
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.
M
Aug 18, 2008 at 5:18 PM
Aren't they limits of sin x and e^(-x)
H
Aug 18, 2008 at 5:36 PM
No idea about that. But I need to know how to display the series for n.
redhat
redhat #4
Aug 18, 2008 at 7:54 PM
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
ashfame #5
Aug 18, 2008 at 8:07 PM
Code:
#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
H
Aug 18, 2008 at 8:13 PM
I haven't been taught functions yet. Any other way around the factorial part?
K
Aug 18, 2008 at 8:33 PM
You want to create 2 for loops, one nested inside the other.
Try it, should work.
ashfame
ashfame #8
Aug 18, 2008 at 8:36 PM
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
H
Aug 18, 2008 at 8:39 PM
I will try it.

I am not aware of logical operator '!'. What does it do?
B
Bandu #10
Aug 18, 2008 at 8:48 PM
@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:
Code:
if(!flag)
	sum=sum+(x^(2n+1))/factorial(2n+1);

New code that does not use factorial:
Code:
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.
H
hitman050 #12
Aug 18, 2008 at 8:50 PM
Can you please explain (!flag)?
ashfame
ashfame #13
Aug 18, 2008 at 8:50 PM
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.

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.
B
Bandu #14
Aug 18, 2008 at 8:59 PM
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.
C
Count0paw #15
Oct 2, 2008 at 10:49 PM
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! + ...