# Help needed to expand sin(x)

This is a discussion on Help needed to expand sin(x) within the C Programming forums, part of the General Programming Boards category; well i am having a bit problem in expanding sin(x). The question says- Q> Write a program in C to ...

1. ## Help needed to expand sin(x)

well i am having a bit problem in expanding sin(x).
The question says-

Q> Write a program in C to compute sin(x) =x - x^3/3! + x^5/5! -x^7/7! +....continue until the value of the next term becomes smaller than 10^-5 (in magnitude) . Test the program for x=1,x=2,x=3.In each case display the number of terms used to obtain the final answer .

I don't know what error i am doing with this code -
Code:
```#include <stdio.h>
#include <conio.h>
#include <math.h>
float denominator(int);
int main()
{
float n, x, a, b, sum = 0;
int i, j, counter = 0;
printf("Enter the number x for sin(x): ");
scanf("%f", &x);

for (i = 1, j = 1; i <= 19, j <= 19; i++, j += 2) {

a = pow(-1, i + 1) * pow(x, j);
b = denominator(j);
n = a / b;

sum = sum + n;

counter++;

}

printf("sin(%f)= %f \n", x, sum);
printf("No. of terms used = %d", counter);
getch();

}

float denominator(int j)
{
int m;
float h = 1;
for (m = 1; m <= j; m++)
h = h * m;
return (h);

}```

2. Are you ever comparing n with 1E-5?

3. no...cuz whenever i use the code
Code:
```while (n>0000.1)
{
sum = sum +n;
counter ++;
}```
I am not getting the proper output...

4. Two things:
*You want 0.00001 instead of 0000.1
*For every second term, n is negative so even if n were -5000 the while loop would stop because -5000 is less than 0.00001. Try fabs(n) instead.

5. "error". What tells you that there is an error?

6. no error in the code sir
error in the answer (not getting exactly what i want)

using
Code:
```while(fabs(n)>0.00001)

{sum=sum+n;

counter++;}```
gives the output :

Enter the number x for sinx (x) =

But even i enter the number x as 1,1.57,2...etc.
I get the output as -
Enter the number x for sinx (x) = 1.57
Press any key to continue ....

8. > continue until the value of the next term becomes smaller than 10^-5

Is
if ( fabs(n)>0.00001 ) break;

Oh, and look up what the comma operator does in your for loop.
It isn't the same as && or ||

9. Originally Posted by Salem
> continue until the value of the next term becomes smaller than 10^-5

Is
if ( fabs(n)>0.00001 ) break;

Oh, and look up what the comma operator does in your for loop.
It isn't the same as && or ||

Code:
```#include <stdio.h>
#include <conio.h>
#include <math.h>
float denominator (int);
int main()
{
float f,n,x,a,b,sum=0;
int i,j,counter=0;
printf ("Enter the number x for sin(x): ");
scanf ("%f",&x);

for (i=1 , j=1;i<=50 , j<=50;i++ , j+=2)
{

a=pow(-1,i+1) * pow(x,j) ;
b=denominator(j);
n=a/b;
f = fabs(n);

while(f>0.00001)

{

sum=sum+n;

counter++; break; }

}

printf ("sin(%f)= %f \n",x,sum);
printf ("No. of terms used = %d",counter);
getch();

}

float denominator (int j)

{
int m ;
float h =1;
for (m=1;m<=j;m++)
h=h*m;
return (h);

}```
This is the right code !! Thanks a lot !!

10. No - it isn't right at all.

It stops you adding minor terms, but you're wasting all those loops calculating terms you're not using.

11. Yeah I see, the break breaks out of the while loop, but not out of the for loop.
This will give the appearance that his program is doing what is asked, but in reality it isn't quite and may give slightly difference results that was asked for.