求s=1+1/2+1/3+......+1/100。
时间: 2023-11-06 08:05:14 浏览: 60
要求 s=1/1 + 1/2 + 1/3 + ... + 1/100 的和。
我们可以使用循环来计算这个和。以下是使用Python语言的示例代码:
```python
s = 0
for i in range(1, 101):
s += 1/i
print(s)
```
运行这段代码,我们可以得到 s 的值为:5.187377517639621
相关问题
C++用whlie循环,1.求s=1+2+3+4+.....+10 2.求s=1+1/2+1/3+.....1/100
好的,我来回答你的问题。
1. 求s=1+2+3+4+.....+10 的代码如下:
```c++
int s = 0;
int i = 1;
while (i <= 10) {
s += i;
i++;
}
cout << "s = " << s << endl;
```
输出结果为:s = 55
2. 求s=1+1/2+1/3+.....1/100 的代码如下:
```c++
double s = 0;
int i = 1;
while (i <= 100) {
s += 1.0 / i;
i++;
}
cout << "s = " << s << endl;
```
输出结果为:s = 5.18738
C++用whlie循环,1.求s=1+2+3+4+.....+10 2.求s=1+1/2+1/3+.....1/100的程序
1. 求s=1+2+3+4+.....+10的程序:
```c++
#include <iostream>
using namespace std;
int main() {
int i = 1, s = 0;
while (i <= 10) {
s += i;
i++;
}
cout << "s = " << s << endl;
return 0;
}
```
输出结果为:s = 55
2. 求s=1+1/2+1/3+.....1/100的程序:
```c++
#include <iostream>
using namespace std;
int main() {
int i = 1;
double s = 0;
while (i <= 100) {
s += 1.0 / i;
i++;
}
cout << "s = " << s << endl;
return 0;
}
```
输出结果为:s = 5.18738
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)