# Thread: sum of the series - loop?

1. ## sum of the series - loop?

Hi guys.

I have to calculate the sum of the following series: MathBin.net - j with the precision of 10^-8

I need to use the following approximation: MathBin.net - approx while the stopping criterion should be: MathBin.net - stopping criterion

The next partial sum should be calculated from the previous one.

The program should be pretty similar to To find the sum of the sine series - free C source codes however here it's the user who enters the number of terms.

How to do this with the other way?

Thanks!

2. The answer is right in front of you with that free source code right there, we're not here to do your homework for you. It doesn't help the learning process if you just search for existing code, either.

3. Yes, you're right. But that program totally differs from mine - I have no idea how to make it without entering the number of terms by myself.

4. So change the condition that causes the program to stop looping.

5. OK, so far i have written this:

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

int main()
{
float sum,epsilon,x,elem;
int k;

sum=0;

do
{
printf("Enter the value of x");
scanf("%d", &x);
if (x<(3.14)) &&  (x>(3.14));
{
printf("X must be between -pi and pi");
}```
Could you help me and tell what to do next?

6. Fix the errors in what you have written. (Compile it, run it, see what happens.)

7. I made some changes, but still there's some error.

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

int main()
{
float sum,epsilon,x,elem;
int k;

sum=0;

do
{
printf("Enter the value of x");
scanf("%d", &x);
}
if x<(3,14) || x>(3,14)
{
printf("X must be between -pi and pi");
}
return 0;
}```

8. Originally Posted by jacek
I made some changes, but still there's some error.

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

int main()
{
float sum,epsilon,x,elem;
int k;

sum=0;

do
{
printf("Enter the value of x");
scanf("%d", &x);
}
if x<(3,14) || x>(3,14)
{
printf("X must be between -pi and pi");
}
return 0;
}```
ok

Code:
`  scanf("%d", &x);`
x---> is a float and why are you using "%d" for a float?

shouldnt it be do with while loop.

%c -- characters
%d -- integers
%f -- floats
what exactly do you mean by this?
Code:
`    if x<(3,14) || x>(3,14)`
I think what you meant was "3.14" instead of "3,14"

9. I have been working a few hours on this. I think that it shows what I want to get. Help will be appreciated.

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

int main()
{
float sum,x,element;
int n;

n=1;
sum=0;

while ((x < 3.14) || (x>3.14))
{
printf("Enter the value of x. It must be between -pi and pi");
scanf("%f", &x);
}

element=sin(x); //first element
sum=element;

while (element>1e-8)
{
n+1
element=(sin(n*x))/(n^3);
printf("Elements %d: %.10f sum:%.10f n:%d", n,element,sum,n);
sum=element+sum;
}

return 0;
}```

10. You can't just add 1 to n. You then have to put that new result somewhere.

To raie something to a power, you need to use pow.

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

int main()
{
float sum,x,element;
int n;

n=1;
sum=0;

do
{
printf("Enter the value of x. It must be between -pi and pi");
scanf("%f", &x);
}
while (x < -atan(1)*4 || x>atan(1)*4);	 //atan(1)*4=3.14...

element=sin(x);	//first element
sum=element;

while (element>1e-8 || element<-1e-8)
{
n=n+1;
element=(sin(n*x))/pow(n,3);
printf("Elements %d: %.10f sum:%.10f n:%d\n", n,element,sum,n);
sum=element+sum;
}

return 0;
}```

13. I guess it jus adds next elements to each other. But it should substract and add alternately. How to do it?

14. That's because you forgot the (-1) raised to the n power.

15. Do you mean switching
Code:
`element=(sin(n*x))/pow(n,3);`
to
Code:
`element=(sin(n*x)*pow(-1,n))/pow(n,3);`
?