# please identify the mistake in the given coding

Discussion in 'C' started by sumedh yadav, Aug 19, 2012.

Joined:
Aug 18, 2012
Messages:
17
0
Trophy Points:
0
Write a function to find value of sin(x) using Expansion Series given below:
Code:
```sin(x) = x - x^3/3! + x^5/5! - x^7/7!......

#include <stdio.h>
#include <math.h>
int factorial(int a) {
int i,k;
for(i=a;i>0;i--) {
k=k*i;
}
return k;
}

float power(float a,float b) {
float i,j;
j=1;
for(i=1;i<=b;i++)  {
j=j*a;
}
return j;
}

int main()  {
float i,x,y,term,sum=0;
printf("Enter the variable x\n");
printf("Enter the prescribed precision\n");
scanf("%f%f",&x,&y);
for(i=1;(term/sum)*100<=y;i++) {
term=power(-1,(i+1))*power(x,(2*i-1))/factorial((2*i-1));
sum+=term;
}
printf("sin(%f)=%f",x,sum);
return 0;
}```

2. ### louies89New Member

Joined:
Oct 4, 2012
Messages:
4
0
Trophy Points:
0
In for(i=1;(term/sum)*100<=y;i++) you have done like(term/sum) which is 0/0 which is not valid mathematical operation.

3. ### louies89New Member

Joined:
Oct 4, 2012
Messages:
4
0
Trophy Points:
0
And why are you dividing it and comparing it with "y"?