用c语言求1+1/2+1/3+……+1/10的结果并输出。
时间: 2023-09-20 10:08:39 浏览: 90
以下是使用C语言求解1+1/2+1/3+……+1/10的结果并输出的代码:
```c
#include <stdio.h>
int main() {
float sum = 0.0;
for(int i = 1; i <= 10; i++) {
sum += 1.0 / i;
}
printf("1+1/2+1/3+...+1/10 = %f", sum);
return 0;
}
```
输出结果为: `1+1/2+1/3+...+1/10 = 2.928968`
相关问题
用c语言求1+1/2!+……+1/n
以下是用C语言求解1+1/2!+……+1/n!的代码:
```
#include <stdio.h>
int main() {
int n, i;
double sum = 1, fact = 1;
printf("请输入n:");
scanf("%d", &n);
for (i = 2; i <= n; i++) {
fact *= i;
sum += 1.0 / fact;
}
printf("1+1/2!+...+1/%d!的值为:%lf", n, sum);
return 0;
}
```
这里使用了一个循环来计算1+1/2!+……+1/n!的值,其中变量`fact`表示阶乘,初始值为1,每次循环将它乘以当前的数`i`,然后用`1.0/fact`计算当前项的值并加到`sum`中。最后,输出结果即可。
用C语言求1+1/2!+……+1/n!
以下是用C语言求解1+1/2!+……+1/n!的代码:
```
#include <stdio.h>
int main() {
int n, i;
double sum = 1, fact = 1;
printf("请输入n:");
scanf("%d", &n);
for (i = 2; i <= n; i++) {
fact *= i;
sum += 1.0 / fact;
}
printf("1+1/2!+...+1/%d!的值为:%lf", n, sum);
return 0;
}
```
这里使用了一个循环来计算1+1/2!+……+1/n!的值,其中变量`fact`表示阶乘,初始值为1,每次循环将它乘以当前的数`i`,然后用`1.0/fact`计算当前项的值并加到`sum`中。最后,输出结果即可。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)