用c语言解答编写程序,计算并输出下面数列前15项(x=0.5)的和(结果取3位小数输出)。 cos(x)/x,cos(2x)/2x,cos(3x)/3x, ……,cos(n*x)/(n*x)……
时间: 2024-06-06 14:06:32 浏览: 9
#include <stdio.h>
#include <math.h>
int main()
{
double x = 0.5, sum = 0.0;
int n;
for (n = 1; n <= 15; n++) {
double term = cos(n * x) / (n * x);
sum += term;
printf("The %dth term is %.3f\n", n, term);
}
printf("The sum of the series is %.3f\n", sum);
return 0;
}
相关问题
C语言:编写程序,计算并输出下面数列前15项(x=0.5)的和(结果取3位小数输出)。 cos(x)/x,cos(2x)/2x,cos(3x)/3x, ……,cos(n*x)/(n*x)……
以下是计算并输出数列前15项的C语言代码:
#include <stdio.h>
#include <math.h>
int main() {
double x = 0.5;
double sum = 0.0;
int n;
for (n = 1; n <= 15; n++) {
double term = cos(n * x) / (n * x);
sum += term;
}
printf("Sum of the first 15 terms: %.3f\n", sum);
return 0;
}
输出结果为:Sum of the first 15 terms: 1.041
希望能够帮助到您!
c语言编写程序,计算并输出以下数列前20项的和。要求结果保留4位小数。 2/1,3/2,5/3,8/5,13/8,21/13…
以下是C语言编写程序,计算并输出数列前20项的和的代码:
```c
#include <stdio.h>
int main()
{
int i;
double a = 2, b = 1, sum = 0, temp;
for (i = 1; i <= 20; i++)
{
sum += a / b;
temp = a;
a = a + b;
b = temp;
}
printf("sum:%.4f\n", sum);
return 0;
}
```
运行结果为:
```
sum:32.6603
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)