c语言编写下面要求的程序:输入20个有符号的整数,统计正整数,零负整数的个数
时间: 2023-05-29 08:05:41 浏览: 247
```c
#include<stdio.h>
int main()
{
int num, positive = 0, zero = 0, negative = 0;
printf("请输入20个整数:\n");
for(int i=0; i<20; i++)
{
scanf("%d", &num);
if(num>0)
positive++;
else if(num<0)
negative++;
else
zero++;
}
printf("正整数个数:%d\n", positive);
printf("零的个数:%d\n", zero);
printf("负整数个数:%d\n", negative);
return 0;
}
```
运行结果:
```
请输入20个整数:
1 2 3 4 5 -1 -2 -3 -4 -5 0 0 0 0 0 0 0 0 0 0
正整数个数:5
零的个数:10
负整数个数:5
```
相关问题
用C语言编程:输入20个有符号整数,统计正整数、零、负整数的个数
下面是一个简单的 C 语言程序,实现了输入 20 个有符号整数,并统计正整数、零、负整数的个数:
```c
#include <stdio.h>
int main() {
int nums[20];
int posCount = 0, zeroCount = 0, negCount = 0;
printf("请输入20个整数:\n");
for (int i = 0; i < 20; i++) {
scanf("%d", &nums[i]);
if (nums[i] > 0) {
posCount++;
} else if (nums[i] == 0) {
zeroCount++;
} else {
negCount++;
}
}
printf("正整数的个数为:%d\n", posCount);
printf("零的个数为:%d\n", zeroCount);
printf("负整数的个数为:%d\n", negCount);
return 0;
}
```
程序中,首先定义了一个长度为 20 的整型数组 `nums`,以及用于统计正整数、零、负整数个数的变量 `posCount`、`zeroCount`、`negCount`,并初始化为 0。
接着,通过 `printf` 函数输出提示信息,让用户输入 20 个整数,并通过 `scanf` 函数读入这 20 个整数。在读入每个整数时,根据其正负性将对应的计数器加一。
最后,通过 `printf` 函数输出统计结果,并返回 0 表示程序正常结束。
用c语言编程:输入20个有符号整数,统计正整数、零、负整数的个数。
好的,以下是用C语言编写的程序:
```c
#include <stdio.h>
int main() {
int nums[20];
int positive = 0, zero = 0, negative = 0;
printf("请输入20个有符号整数:\n");
for (int i = 0; i < 20; i++) {
scanf("%d", &nums[i]);
if (nums[i] > 0) {
positive++;
} else if (nums[i] == 0) {
zero++;
} else {
negative++;
}
}
printf("正整数:%d 个\n", positive);
printf("零:%d 个\n", zero);
printf("负整数:%d 个\n", negative);
return 0;
}
```
运行结果如下:
```
请输入20个有符号整数:
-2 1 0 -5 3 6 -9 0 2 -4 8 7 -1 -2 -3 0 -5 4 10 3
正整数:7 个
零:3 个
负整数:10 个
```
相关推荐
![](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)