请用C语言写下面的代码,要求如下 输入若干个整数,以0结束,统计正数和负数的个数。
时间: 2023-05-24 07:02:29 浏览: 1149
```
#include <stdio.h>
int main() {
int num, posCount = 0, negCount = 0;
printf("请输入若干个整数,以0结束:\n");
scanf("%d", &num);
while (num != 0) {
if (num > 0) {
posCount++;
} else {
negCount++;
}
scanf("%d", &num);
}
printf("正数个数:%d\n负数个数:%d", posCount, negCount);
return 0;
}
```
相关问题
c语言输入一系列整数,分别统计其中正数和负数的个数,当输入0时结束
你好!这个问题可以通过C语言编程实现。
下面是一个示例代码:
```c
#include <stdio.h>
int main()
{
int num, positive = 0, negative = 0;
printf("请输入一系列整数,以0结束:\n");
do {
scanf("%d", &num);
if (num > 0)
{
positive++;
}
else if (num < 0)
{
negative++;
}
} while (num != 0);
printf("输入的正数个数为:%d,负数个数为:%d\n", positive, negative);
return 0;
}
```
希望这个回答能解决您的问题。
用C语言写统计正数、负数和零的个数 从键盘输入10 个整数,统计其中正数、负数和零的个数,并在屏幕上依次输出
可以使用以下代码实现:
```c
#include <stdio.h>
int main() {
int nums[10];
int positive = 0, negative = 0, zero = 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++;
} else if (nums[i] < 0) {
negative++;
} else {
zero++;
}
}
// 输出结果
printf("正数个数:%d\n", positive);
printf("负数个数:%d\n", negative);
printf("零的个数:%d\n", zero);
return 0;
}
```
运行程序后,输入10个整数,程序会计算并输出正数个数、负数个数和零的个数。
阅读全文
相关推荐













