输入x的值,调用函数计算sinx=x/1-x的3次方/3!+x的5次方/5!-x的7次方/7!+……+xn/n!<10的-6次方
帮帮忙
输入x的值,调用函数计算sinx=x/1-x的3次方/3!+x的5次方/5!-x的7次方/7!+……+xn/n!<10的-6次方
帮帮忙
#include <stdio.h>
double sa(double x, int n)
{
double a = 1;
for(; n > 0; n--)
a *= x;
return a;
}
double sb(double x, int n)
{
double b = 1;
for(; n > 0; n--)
b *= n;
return b;
}
double sin(double x)
{
int n = 1;
double a = 1, b = 1, i = 1, sum = 0;
while(a/b > 1e-6)
{
a = sa(x, n);
b = sb(x, n);
sum += i*a/b;
n += 2;
i *= -1;
}
return sum;
}
void main()
{
double x, y;
scanf("%lf", &x);
y = sin(x);
printf("y=sin(%lf)= %lf\n", x, y);
}