用C语言求级数cos(x) ≈ 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8! - ...,要求最后一项小于10-6结束
时间: 2023-11-19 13:04:35 浏览: 63
A级景区数据文件json
Sure, I can solve this problem for you. Here's the C code:
#include <stdio.h>
#include <math.h>
int main()
{
double x, term, sum;
int n;
printf("Please enter the value of x in radians: ");
scanf("%lf", &x);
term = 1.0;
sum = term;
for (n = 1; fabs(term) > 1e-6; n++) {
term = -term * x * x / (2 * n) / (2 * n - 1);
sum += term;
}
printf("cos(%.2lf) = %.6lf\n", x, sum);
return 0;
}
Note that this code uses the math library function fabs() to calculate the absolute value of a floating-point number.
阅读全文