C语言编写程序,从输入的整数中统计大于0和小于0的个数,用0作为结束输入的数据。。。求解。。。谢谢了。

2025-03-19 07:18:59
推荐回答(2个)
回答1:

#include 

int main() {

int plus = 0,negative = 0,num;

while(1) {

printf("请输入 : ");

scanf("%d",&num);

if(num == 0) break;

if(num > 0) plus++;

else negative++;

}

printf("大于0的数是%d个\n",plus);

printf("小于0的数是%d个\n\n",negative);

return 0;

}

回答2:

#include
void main()
{
int a[100] = {0}, i = 0, z=0, f=0, s;
printf("输入一个整数按回车进行下次输入\n");  
while (1)
{
scanf("%d",&a[i]);
if (a[i] == 0) { break; }
i++;
}
for (s = 0; s < i; s++)
{
if (a[s] < 0) { f = f + 1; }
if (a[s] > 0) { z = z + 1; }
}
printf("正数的个数有%d个\n负数的个数有%d个\n", z, f);
while (1);
}