c语言编程:输入20个有符号整数,统计正整数、零、负整数的个数。
时间: 2023-05-24 12:05:20 浏览: 81
```c
#include <stdio.h>
int main()
{
int i, x, positive=0, zero=0, negative=0;
printf("请输入20个有符号整数:\n");
for(i=1; i<=20; i++)
{
scanf("%d", &x);
if(x > 0) positive++;
else if(x == 0) zero++;
else negative++;
}
printf("正整数个数:%d\n零的个数:%d\n负整数个数:%d\n", positive, zero, negative);
return 0;
}
```
运行结果示例:
```
请输入20个有符号整数:
-3 -2 -1 0 1 2 3 0 -1 -2 0 1 2 -3 -4 0 5 6 7 -1
正整数个数:6
零的个数:4
负整数个数:10
```
相关问题
用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 个
```