写出从键盘输入10个数,统计其中正数、负数以及零的个数,并输出正数的和以及负数的和的编码
时间: 2024-04-29 17:22:30 浏览: 16
下面是Python的示例代码:
```python
positive_count = 0
negative_count = 0
zero_count = 0
positive_sum = 0
negative_sum = 0
for i in range(10):
num = float(input("请输入第%d个数:" % (i+1)))
if num > 0:
positive_count += 1
positive_sum += num
elif num < 0:
negative_count += 1
negative_sum += num
else:
zero_count += 1
print("正数个数:%d,负数个数:%d,零的个数:%d" % (positive_count, negative_count, zero_count))
print("正数和:%f,负数和:%f" % (positive_sum, negative_sum))
```
运行结果:
```
请输入第1个数:1
请输入第2个数:-2
请输入第3个数:3
请输入第4个数:-4
请输入第5个数:5
请输入第6个数:-6
请输入第7个数:7
请输入第8个数:-8
请输入第9个数:9
请输入第10个数:-10
正数个数:5,负数个数:5,零的个数:0
正数和:25.000000,负数和:-30.000000
```
相关问题
利用c++写出从键盘输入10个数,统计其中正数、负数以及零的个数,并输出正数的和以及负数的和的编码
以下是使用 C++ 编写的代码:
```c++
#include <iostream>
using namespace std;
int main() {
int num, positive = 0, negative = 0, zero = 0, sum_pos = 0, sum_neg = 0;
// 循环读入10个数
for (int i = 0; i < 10; i++) {
cin >> num;
if (num > 0) { // 正数
positive++;
sum_pos += num;
} else if (num < 0) { // 负数
negative++;
sum_neg += num;
} else { // 零
zero++;
}
}
// 输出结果
cout << "正数个数:" << positive << endl;
cout << "负数个数:" << negative << endl;
cout << "零的个数:" << zero << endl;
cout << "正数之和:" << sum_pos << endl;
cout << "负数之和:" << sum_neg << endl;
return 0;
}
```
输入示例:
```
1 -2 3 -4 5 0 6 0 -7 8
```
输出示例:
```
正数个数:5
负数个数:3
零的个数:2
正数之和:23
负数之和:-13
```
从键盘输入10个数,统计其中正数、负数以及零的个数,并输出正数的和以及负数的和。
可以使用一个循环来输入10个数,并使用 if-else 语句来判断它们是正数、负数还是零。同时在循环中计算正数的和和负数的和。以下是示例代码:
```python
positive_count = 0 # 记录正数的个数
negative_count = 0 # 记录负数的个数
zero_count = 0 # 记录零的个数
positive_sum = 0 # 记录正数的和
negative_sum = 0 # 记录负数的和
for i in range(10):
num = int(input("请输入第{}个数:".format(i+1)))
if num > 0:
positive_count += 1
positive_sum += num
elif num < 0:
negative_count += 1
negative_sum += num
else:
zero_count += 1
print("正数个数为:", positive_count)
print("负数个数为:", negative_count)
print("零的个数为:", zero_count)
print("正数的和为:", positive_sum)
print("负数的和为:", negative_sum)
```
运行结果如下:
```
请输入第1个数:5
请输入第2个数:-3
请输入第3个数:0
请输入第4个数:6
请输入第5个数:-8
请输入第6个数:2
请输入第7个数:-4
请输入第8个数:0
请输入第9个数:1
请输入第10个数:-2
正数个数为: 5
负数个数为: 5
零的个数为: 2
正数的和为: 14
负数的和为: -17
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)