for regular series caluclation

we could use this

``````{
double summand = 1;
int n = 1;
double sum = 1;

do
{
summand = summand * x / n;
sum = sum + summand;
n--;
}
while (summand > 0.00001);

return sum;
}``````

i.e for e^x= 1+x+(x^2)/2! + (x^3)/3! ....... so on

how would u change the above code if

e^x= 1-x+(x^2)/2! - (x^3)/3! +(x^4)/4! - (x^5)/5! ....... so on

in which the sum is alternated ... any help would be great ...

cheers

## Recommended Answers

Keep a flag variable which will decide whether the operation should be addition or subtraction.

``````{
double summand = 1;
int n = 1;
double sum = 1;
bool flag = true;
do
{
summand = summand * x / n;

[B]      if (flag)
sum += summand; …``````

Maybe you need to look a bit harder at the code I posted. It takes care of changing the flag variable. Look out for the part posted in bold...

Maybe something like this:

``````double factorial (double value)
{
double result = 1.0;
while (fabs (value) > 0)
{
result *= value--;
}
return result;
}

int main ()
{
double summand = 0.0, sum = 0.0;
int value = 0, current = 0;
bool flag = …``````

## All 11 Replies

Keep a flag variable which will decide whether the operation should be addition or subtraction.

``````{
double summand = 1;
int n = 1;
double sum = 1;
bool flag = true;
do
{
summand = summand * x / n;

[B]      if (flag)
sum += summand;
else
sum -= summand;
flag = !flag;[/B]

n--;
}
while (summand > 0.00001);

return sum;
}``````

how would u alternate the flag variable everytime ??

is it by using the loop

Maybe you need to look a bit harder at the code I posted. It takes care of changing the flag variable. Look out for the part posted in bold...

commented: Rep for a hard-working (and patient!) moderator... --Joe(y) +8

yea but u have to alternate the ++ and -- everytime ... i tried the code but does not give correct output

how would u do it for

Approximate sine of θ = θ – (θ3/3!) + (θ5/5!) - (θ7/7!) + (θ9/9!)

The alteration is happening alright. Your logic is wrong. Look at it once again. Are you sure you are doing what the equation demands ?

wt would i need to do for the sin equation i mean what summand equation would i need

Maybe something like this:

``````double factorial (double value)
{
double result = 1.0;
while (fabs (value) > 0)
{
result *= value--;
}
return result;
}

int main ()
{
double summand = 0.0, sum = 0.0;
int value = 0, current = 0;
bool flag = true;

cout << "Enter the power of e whose answer you want: " ;
cin >> value;
getchar ();

do
{
summand = pow ((double)value, (double)current) / factorial (current);
if (flag)
sum += summand;
else
sum -= summand;

flag = !flag;
++current;
}
while (summand > 0.00001);
cout << "\nAnswer is : " << sum;

getchar ();
return 0;
}``````

it still doesn seem to work when i compute sin 20

here is the link to what i really wanted

Just replace the `++current` in my previous code with `current += 2` and make the data type of the variable `value` as `double` and you should be good to go.

thanks for the help

hey uoit fella..lol..can't we just use pow(theta,n)?? i think we can..

good luck! about 7 hours to go and we still didnt figure it out! lol

Be a part of the DaniWeb community

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