c语言,使用函数,输入一个整数,判断其是正数还是负数
时间: 2024-05-09 07:17:34 浏览: 24
好的,这个问题可以回答。您可以使用如下的 C 语言代码来完成这个功能:
```c
#include <stdio.h>
void checkNumber(int num) {
if (num > 0) {
printf("这是一个正数\n");
} else if (num < 0) {
printf("这是一个负数\n");
} else {
printf("这是零\n");
}
}
int main() {
int num;
printf("请输入一个整数:");
scanf("%d", &num);
checkNumber(num);
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语言输入10个整数,统计并输出正数、负数和零的个数
### 回答1:
可以使用一个循环遍历10次输入的整数,并用if语句判断每个数的正负性或是否为零,然后分别累加计数器。最后输出三个计数器的值就是正数、负数和零的个数。
代码如下:
```c
#include <stdio.h>
int main() {
int num, positive = 0, negative = 0, zero = 0;
for (int i = 0; i < 10; i++) {
printf("请输入第%d个整数:", i+1);
scanf("%d", &num);
if (num > 0) {
positive++;
} else if (num < 0) {
negative++;
} else {
zero++;
}
}
printf("正数个数:%d\n负数个数:%d\n零的个数:%d\n", positive, negative, zero);
return 0;
}
```
运行结果:
```
请输入第1个整数:2
请输入第2个整数:-5
请输入第3个整数:0
请输入第4个整数:7
请输入第5个整数:-3
请输入第6个整数:0
请输入第7个整数:8
请输入第8个整数:-1
请输入第9个整数:6
请输入第10个整数:-9
正数个数:5
负数个数:4
零的个数:2
```
### 回答2:
要统计10个整数中的正数、负数和零的个数,可以使用C语言编程语言来实现。
首先,我们需要声明并初始化三个变量,分别用于存储正数个数(positiveCount)、负数个数(negativeCount)和零的个数(zeroCount)。这三个变量都需要初始化为0。
然后,使用循环语句来读取和判断输入的10个整数。可以使用for循环来重复执行输入的过程,循环变量遍历从1到10的整数。
在每次循环中,使用scanf函数读取用户输入的整数,并使用if语句判断该整数的正负性。如果输入的数大于0,则将positiveCount变量加1;如果输入的数小于0,则将negativeCount变量加1;如果输入的数等于0,则将zeroCount变量加1。
当循环结束后,使用printf函数将统计结果输出到屏幕上,分别打印正数的个数、负数的个数和零的个数。
下面是一个示例代码的实现:
```c
#include <stdio.h>
int main() {
int positiveCount = 0;
int negativeCount = 0;
int zeroCount = 0;
int number;
for(int i = 0; i < 10; i++) {
printf("请输入第%d个整数:", i + 1);
scanf("%d", &number);
if(number > 0) {
positiveCount++;
} else if(number < 0) {
negativeCount++;
} else {
zeroCount++;
}
}
printf("正数的个数:%d\n负数的个数:%d\n零的个数:%d\n", positiveCount, negativeCount, zeroCount);
return 0;
}
```
运行以上代码,按照提示输入10个整数,程序将统计并输出正数、负数和零的个数。
### 回答3:
可以通过使用循环和条件语句来解决这个问题。首先,我们定义三个变量来保存正数、负数和零的个数,初始值都为0。然后,使用一个for循环来读取输入的10个整数。
在每次循环中,我们使用scanf函数读取一个整数,然后使用if语句进行判断。如果读取的整数大于0,则将正数的计数器加1;如果小于0,则将负数的计数器加1;如果等于0,则将零的计数器加1。
循环结束后,我们可以通过输出正数、负数和零的计数器的值来得到结果。
下面是具体的代码实现:
#include<stdio.h>
int main()
{
int positive_count = 0; // 正数的个数
int negative_count = 0; // 负数的个数
int zero_count = 0; // 零的个数
int i, num;
printf("请输入10个整数:\n");
for(i = 0; i < 10; i++)
{
scanf("%d", &num);
if(num > 0)
{
positive_count++;
}
else if(num < 0)
{
negative_count++;
}
else
{
zero_count++;
}
}
printf("正数的个数:%d\n", positive_count);
printf("负数的个数:%d\n", negative_count);
printf("零的个数:%d\n", zero_count);
return 0;
}
运行代码后,用户需要输入10个整数,程序会统计并输出正数的个数、负数的个数和零的个数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)