# alternating series

Page 1 of 1

## 5 Replies - 15691 Views - Last Post: 18 December 2008 - 11:20 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=77337&amp;s=29fa2027dfc8f1fea4bf87ec15edc924&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 progstud

Reputation: 0
• Posts: 36
• Joined: 21-November 08

# alternating series

Posted 18 December 2008 - 08:13 AM

Hi guys. I am finding it very difficult to understand the logic how to code this alternating series. e.g 1+x-(x^2)+(x^3)...
Any help pls. This is what I've got.
```#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main(void)
{
int number;
int i;
int sum=0;
int power;
int value;

printf("Enter a number");
scanf("%d",&number);

for(i=0;i<=3;i++)
{

power =pow(number,i);
sum= sum +power;
number = number*(-1);
}
printf("The sum is %d",sum);
return 0;
}
```

Is This A Good Question/Topic? 0

## Replies To: alternating series

### #2 progstud

Reputation: 0
• Posts: 36
• Joined: 21-November 08

## Re: alternating series

Posted 18 December 2008 - 08:51 AM

any help pls? Thanks a lot.

### #3 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: alternating series

Posted 18 December 2008 - 10:52 AM

1+x-(x^2)+(x^3)...

What are the first two terms? The result you are going to calculate needs to include them.

I would not use integers for this problem but rather doubles since that is what pow() is going to return as it's result.
See pow()

### #4 progstud

Reputation: 0
• Posts: 36
• Joined: 21-November 08

## Re: alternating series

Posted 18 December 2008 - 11:01 AM

This question I am asking is part of the cosine series(without math library). I managed to get here. I am not sure of the output I am having. Am I alternating the sign correctly.Thanks a lot.
```#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>

double cos(double x,double N);

void main()
{
double num;
double n;
printf("Enter the value and accuracy");
scanf("%lf%lf",&num,&n);
printf("value is %f",cos(num,n));

}

double cos(double x,double N)
{
double sum=0;
double sign=1;
double term=0;
int i;
sum=sum+1;

for(i=2;i<=N;i+=2)
{
sign*=-1;
term=(sign*pow(x,i))/factorial(i);
sum+=term;
}
return sum;
}

int factorial(int number)
{
if(number<=1)
{
return 1;
}else
{
return number*factorial(number-1);
}//end if...

}
```

### #5 amir_ju

Reputation: 17
• Posts: 82
• Joined: 20-June 08

## Re: alternating series

Posted 18 December 2008 - 11:17 AM

cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...
are you sure you you want to evaluate 1+x-(x^2)+(x^3)... ?

This post has been edited by amir_ju: 18 December 2008 - 11:18 AM

### #6 progstud

Reputation: 0
• Posts: 36
• Joined: 21-November 08

## Re: alternating series

Posted 18 December 2008 - 11:20 AM

sorry I haven't been so clear. I am trying to eveluate the cosine series.