I'm trying to calculate the sin with the Taylor series but for some reason it doesn't work and it's returning big numbers (bigger than 1). I saw some examples on google but I don't see where I did a mistake. here's the code:
#include <stdio.h>
#include <math.h>
int factorial(int n) {
int i, f=1;
for (i=1; i<=n; i++) {
f= f*i;
}
return (f);
}
int main() {
int x, i, j=0;
float s=0;
printf("write a number to calculate sin(x)\n");
scanf("%d",&x);
for(i=1;i<=x ;i=i+2) {
if(j%2==0){
s -= pow(x, i)/factorial(i);
}
else{
s += pow(x, i)/factorial(i);
}
j++;
}
printf("sin(%d)=%f \n", x, s);
}
here's an example output: sin(6)=-34.799999
but in a calculator: sin(6)=-0.27941549819