编写代码,分别统计输入的正数和负数的个数,直到输入0结束。
时间: 2024-05-11 13:15:45 浏览: 17
好的,这是一个计算问题,我可以为您提供以下代码示例:
```
#include <stdio.h>
int main () {
int positive_count = 0, negative_count = 0, num;
do {
printf("请输入一个整数:");
scanf("%d", &num);
if (num > 0) {
positive_count++;
} else if (num < 0) {
negative_count++;
}
} while (num != 0);
printf("正数个数:%d,负数个数:%d\n", positive_count, negative_count);
return 0;
}
```
相关问题
C语言编写代码:输入15个整数,统计其中正数,负数,以及0的个数
### 回答1:
以下是C语言的代码,用于输入15个整数,统计其中正数、负数以及0的个数:
```
#include <stdio.h>
int main() {
int num, positive = 0, negative = 0, zero = 0;
printf("请输入15个整数:\n");
for (int i = 1; i <= 15; i++) {
scanf("%d", &num);
if (num > 0) {
positive++;
} else if (num < 0) {
negative++;
} else {
zero++;
}
}
printf("正数个数:%d\n负数个数:%d\n0的个数:%d\n", positive, negative, zero);
return 0;
}
```
该代码通过一个for循环,输入15个整数并统计它们的正数、负数和0的个数。其中,正数个数存储在变量positive中,负数个数存储在变量negative中,0的个数存储在变量zero中。最后,通过printf函数输出统计结果。
### 回答2:
下面是统计正数、负数和0个数的C语言代码:
```c
#include <stdio.h>
int main() {
int nums[15];
int i, positiveCount = 0, negativeCount = 0, zeroCount = 0;
printf("请输入15个整数:\n");
// 循环输入15个整数
for (i = 0; i < 15; i++) {
scanf("%d", &nums[i]);
}
// 统计正数、负数和0的个数
for (i = 0; i < 15; i++) {
if (nums[i] > 0) {
positiveCount++;
} else if (nums[i] < 0) {
negativeCount++;
} else {
zeroCount++;
}
}
printf("正数的个数:%d\n", positiveCount);
printf("负数的个数:%d\n", negativeCount);
printf("0的个数:%d\n", zeroCount);
return 0;
}
```
这段代码首先声明了一个大小为15的整数数组`nums`用于存放输入的15个整数。然后通过`for`循环逐个输入15个整数,并使用另一个`for`循环遍历整数数组,判断每个元素的正负性或者是否为0,并根据判断结果对相应的计数变量进行增加。最后,输出正数、负数和0的个数。
### 回答3:
你好!以下是一个C语言编写的代码示例,用于输入15个整数并统计其中正数、负数以及0的个数:
```c
#include <stdio.h>
int main() {
int num, positive_count = 0, negative_count = 0, zero_count = 0;
printf("请输入15个整数:\n");
for (int i = 1; i <= 15; i++) {
printf("整数 %d: ", i);
scanf("%d", &num);
if (num > 0) {
positive_count++;
} else if (num < 0) {
negative_count++;
} else {
zero_count++;
}
}
printf("\n正数的个数:%d\n", positive_count);
printf("负数的个数:%d\n", negative_count);
printf("0的个数:%d\n", zero_count);
return 0;
}
```
这段代码首先定义了整数变量`num`,以及统计正数、负数和0的变量 `positive_count`、`negative_count` 和 `zero_count`。
接下来,使用 `for` 循环要求用户输入15个整数,并使用 `scanf` 函数将输入的整数保存在 `num` 变量中。
在每次迭代过程中,代码会判断 `num` 变量的值,并根据其正负或为0,分别将对应的计数变量进行递增。
循环结束后,使用 `printf` 函数输出正数、负数和0的个数。
最后,`main` 函数返回0,表示程序正常结束。
希望这段代码能够满足您的需求,如果有任何疑问,请随时向我提问!
用C语言编写代码:输入数,当输入0时则结束,统计其中正数,负数,以及0的个数
以下是使用C语言编写的代码来实现上述功能:
```
#include <stdio.h>
int main() {
int num, positive = 0, negative = 0, zero = 0;
printf("请输入数字,以0结束:\n");
do {
scanf("%d", &num);
if (num > 0) {
positive++;
} else if (num < 0) {
negative++;
} else {
zero++;
}
} while (num != 0);
printf("输入的正数个数为:%d\n", positive);
printf("输入的负数个数为:%d\n", negative);
printf("输入的零的个数为:%d\n", zero);
return 0;
}
```
代码中定义了一个整数变量num来接收用户输入的数字,同时定义了三个计数变量positive、negative和zero来统计输入的正数、负数和零的个数。
程序通过do-while循环来实现反复输入数字的功能,当输入的数字为0时,循环结束。在每次输入数字后,程序会根据数字的值来对应地将计数变量增加1。最后,程序输出统计结果。
希望这个代码能够帮到你!
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)