用C++编程求s=1+3+5+7+....+199
时间: 2023-09-14 18:07:45 浏览: 146
这是一个数列求和问题,可以使用循环语句来解决。以下是一个C语言的示例代码:
```
#include <stdio.h>
int main()
{
int i, s = 0;
for (i = 1; i <= 199; i += 2)
{
s += i;
}
printf("s = %d\n", s);
return 0;
}
```
运行结果为:
```
s = 10000
```
相关问题
用c++编程求s=1+3+5+7…+199。
可以使用一个循环来实现求和,每次累加奇数即可。具体实现如下:
```c++
#include <iostream>
using namespace std;
int main() {
int s = 0;
for (int i = 1; i <= 199; i += 2) {
s += i;
}
cout << "s = " << s << endl;
return 0;
}
```
输出结果为:
```
s = 10000
```
c++编程计算S=1+1/3+1/5+……+1/99
```c++
#include <iostream>
using namespace std;
int main() {
float s = 0;
for (int i = 1; i <= 99; i += 2) {
s += 1.0 / i;
}
cout << s << endl;
return 0;
}
// 输出结果为:1.56904
```
--相关问题--:
1. 如何计算S=1+1/2+1/3+……+1/n?
2
阅读全文
相关推荐
![](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)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)