# Trouble with calculating a series in C, help me find my mistake.

Page 1 of 1

## 5 Replies - 457 Views - Last Post: 29 January 2016 - 11:24 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=387983&amp;s=39111b33c7bb17e3fbc26cdbedb6d86e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 AggelosAbsent

Reputation: 0
• Posts: 2
• Joined: 28-January 16

# Trouble with calculating a series in C, help me find my mistake.

Posted 28 January 2016 - 06:24 PM

So they gave us this assingment in university:

i)Create a program that asks the user to input a number between 2 and 5. If the number given isn't between those two numbers make it so the program asks the user for a new number until it is larger than two and smaller than 5.
ii)e^x=x^0/0! + x^1/1! + x^2/2! + ...
iii)Create a program that calculates the above series until x^n/n!<=0.0001 and then print the value of x given, e^x, and the last term which was added.

I did what I was asked, but when I run the program it gave me a false result. Also it says that the last term which was added is negative! I struggled for hours to figure out where my mistake was, I even had the teacher take a look at it, but we couldn't find anything. Don't forget that you need to use the command " gcc -o ..... -lm" to compile this program. Please help me find my mistake.

```#include <stdio.h>
#include <math.h>

int main()
{

int i, fact;
double x,a,e,sum;

printf(" \n Input a real number between 2 and 5 : ");
scanf("%lf%, &x);

while(x<=2 || x>=5)
{
printf("\n Input another real number thst fits the conditions stated above : ");
scanf("%lf%, &x);
}

i=1;
a=1.0;
fact=1;
e=0.0001;
sum=0.0;

while(a>=e)
{
a=pow(x,i)/fact;
i++;
fact=fact*i;
sum=sum+a;
}

printf("\n x=%lf, e^x=%lf and last term which was added is a=%lf \n",x,sum,a);

return 0;
}
```

Is This A Good Question/Topic? 0

## Replies To: Trouble with calculating a series in C, help me find my mistake.

### #2 kaa

Reputation: 225
• Posts: 805
• Joined: 15-April 11

## Re: Trouble with calculating a series in C, help me find my mistake.

Posted 28 January 2016 - 09:06 PM

Part of your problem is that your fact variable is overflowing the maximum value of an int, since 13! = 6,227,020,800, whereas the biggest int that your computer can handle is probably 2,147,483,647. Declare fact to be a long int or a long long int to avoid that.

After you make that change I think you'll be able to find the error in your calculation yourself.

### #3 Skydiver

• Code herder

Reputation: 6111
• Posts: 21,038
• Joined: 05-May 12

## Re: Trouble with calculating a series in C, help me find my mistake.

Posted 28 January 2016 - 09:15 PM

Your initial value of sum looks wrong. Your loop assumes that it is computing the i-th term where i > 0. So that means that before going into the loop, sum should be initialized to the 0-th term: x^0/0!. x^0 == 1. 0! == 1. Why is it set to zero.

Next, your loop condition is incorrect. You are support to stop when your term is less than or equal to your epsilon value. Your loop condition continues while it is greater than or equal.

But lastly, and the to the point of your question about the negative term: I suspect that you are overflowing the integer.

### #4 AggelosAbsent

Reputation: 0
• Posts: 2
• Joined: 28-January 16

## Re: Trouble with calculating a series in C, help me find my mistake.

Posted 29 January 2016 - 08:43 AM

Actually, I declared that fact and i were real numbers (double type) and the problem was sort of solved (it doesn't give the 100% correct answer, but it is close enough, about 0.5 smaller).

### #5 Skydiver

• Code herder

Reputation: 6111
• Posts: 21,038
• Joined: 05-May 12

## Re: Trouble with calculating a series in C, help me find my mistake.

Posted 29 January 2016 - 09:06 AM

Check your calculations for the first few iterations by hand. It'll help you find the issue. I realy doubt that the floating point rounding errors would give you as much as 0.5 error.

### #6 horace

• D.I.C Lover

Reputation: 768
• Posts: 3,832
• Joined: 25-October 06

## Re: Trouble with calculating a series in C, help me find my mistake.

Posted 29 January 2016 - 11:24 PM

AggelosAbsent, on 29 January 2016 - 04:43 PM, said:

Actually, I declared that fact and i were real numbers (double type) and the problem was sort of solved (it doesn't give the 100% correct answer, but it is close enough, about 0.5 smaller).

not sure why your result is incorrect if you correct the code as indicated in previous posts, e.g. change fact to a float and initiaalise sum to
```sum=1;
```

if I then run your code I get e^3 = 20.085523

you can simplify your code (removing the need to use exp() and variable fact) by making use of the relationship between sucessive terms in the series
term(n) = term (n-1) * x / n