.从键盘输入10个整数,统计其中正数、负数和零的个数,并在屏幕上输出。 数组方法写
时间: 2024-02-06 16:11:46 浏览: 43
好的,下面是用数组方法实现的代码:
```c
#include <stdio.h>
int main() {
int nums[10];
int positive = 0, negative = 0, zero = 0;
printf("请输入10个整数:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &nums[i]);
if (nums[i] > 0) {
positive++;
} else if (nums[i] < 0) {
negative++;
} else {
zero++;
}
}
printf("正数个数:%d\n", positive);
printf("负数个数:%d\n", negative);
printf("零的个数:%d\n", zero);
return 0;
}
```
这个程序先定义了一个长度为10的整型数组`nums`,然后通过一个循环读入10个整数。在读入每个整数的同时,根据其正负性或是否为零,更新`positive`、`negative`和`zero`的值。最后输出这三个变量的值即可。
相关问题
.从键盘输入10个整数,统计其中正数、负数和零的个数,并在屏幕上输出。数组方法写
可以使用一个大小为3的数组来分别存储正数、负数和零的个数,然后遍历输入的10个整数进行统计。具体实现如下:
```c
#include <stdio.h>
int main() {
int nums[10];
int count[3] = {0, 0, 0}; // 存储正数、负数和零的个数
// 读取输入的10个整数
for (int i = 0; i < 10; i++) {
printf("请输入第%d个整数:", i + 1);
scanf("%d", &nums[i]);
}
// 统计正数、负数和零的个数
for (int i = 0; i < 10; i++) {
if (nums[i] > 0) {
count[0]++; // 正数个数加1
} else if (nums[i] < 0) {
count[1]++; // 负数个数加1
} else {
count[2]++; // 零的个数加1
}
}
// 输出统计结果
printf("正数个数:%d\n", count[0]);
printf("负数个数:%d\n", count[1]);
printf("零的个数:%d\n", count[2]);
return 0;
}
```
运行程序后,依次输入10个整数,程序会统计其中正数、负数和零的个数并输出。
C语言.从键盘输入10个整数,统计其中正数、负数和零的个数,并在屏幕上输出。 数组方法写
可以使用一个数组来存储输入的10个整数,然后遍历数组,统计正数、负数和零的个数,最后输出结果。
下面是一个示例代码:
```c
#include <stdio.h>
int main() {
int nums[10]; // 定义一个包含10个整数的数组
int positive_count = 0; // 正数个数
int negative_count = 0; // 负数个数
int zero_count = 0; // 零的个数
// 从键盘输入10个整数
printf("请输入10个整数:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &nums[i]);
}
// 统计正数、负数和零的个数
for (int i = 0; i < 10; i++) {
if (nums[i] > 0) {
positive_count++;
} else if (nums[i] < 0) {
negative_count++;
} else {
zero_count++;
}
}
// 输出结果
printf("正数个数:%d\n", positive_count);
printf("负数个数:%d\n", negative_count);
printf("零的个数:%d\n", zero_count);
return 0;
}
```
这个程序首先定义了一个包含10个整数的数组,然后使用 `for` 循环从键盘输入这10个整数。接着,使用另一个 `for` 循环遍历数组,统计正数、负数和零的个数,并将结果输出到屏幕上。
相关推荐
![c](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)