用C语言编一程序判断输入整数的正负性和奇偶性
时间: 2023-04-08 10:05:16 浏览: 194
我可以回答这个问题。以下是一个用C语言编写的程序,可以判断输入整数的正负性和奇偶性:
#include <stdio.h>
int main() {
int num;
printf("请输入一个整数:");
scanf("%d", &num);
if (num > 0) {
printf("%d是正数\n", num);
} else if (num < 0) {
printf("%d是负数\n", num);
} else {
printf("输入的是0\n");
}
if (num % 2 == 0) {
printf("%d是偶数\n", num);
} else {
printf("%d是奇数\n", num);
}
return 0;
}
相关问题
C语言编程判断输入整数x的正负性和奇偶性。 **输入格式要求:"%d" 提示信息:"Enter a integer:" **输出格式要求:"The integer is a plus even number\n" "This integer is a plus odd number\n" "This integer is a minus even number\n" "This integer is a minus odd number\n" "This integer is zero.\
以下是 C 语言代码实现:
```c
#include <stdio.h>
int main() {
int x;
printf("Enter an integer: ");
scanf("%d", &x);
if (x > 0) {
if (x % 2 == 0) {
printf("The integer is a plus even number\n");
} else {
printf("This integer is a plus odd number\n");
}
} else if (x < 0) {
if (x % 2 == 0) {
printf("This integer is a minus even number\n");
} else {
printf("This integer is a minus odd number\n");
}
} else {
printf("This integer is zero.\n");
}
return 0;
}
```
代码逻辑比较简单,就是先判断正负性,再判断奇偶性,最后输出相应的结果。其中 `%d` 表示输入的是整数,`scanf` 函数用于输入。输出格式要求依次判断即可。
用C语言编程判断输入整数的正负性和奇偶性。 **输入格式要求:"%d" 提示信息:"Input m:" **输出格式要求: 如果是负偶数,则输出"%d is a negative even\n" 如果是负奇数,则输出"%d is a negative odd\n" 如果是正偶数,则输出"%d is a positive even\n" 如果是正奇数,则输出"%d is a positive odd\n" 如果是0,则输出"%d is zero. It is an even\n" 程序运行示例1: Input m:6↙ 6 is a positive even 程序运行示例2: Input m:0↙ 0 is zero. It is an even
以下是C语言的代码实现:
```c
#include <stdio.h>
int main() {
int m;
printf("Input m:");
scanf("%d", &m);
if (m == 0) {
printf("%d is zero. It is an even\n", m);
} else if (m > 0) {
if (m % 2 == 0) {
printf("%d is a positive even\n", m);
} else {
printf("%d is a positive odd\n", m);
}
} else {
if (m % 2 == 0) {
printf("%d is a negative even\n", m);
} else {
printf("%d is a negative odd\n", m);
}
}
return 0;
}
```
程序先提示用户输入一个整数m,然后根据m的值判断其正负性和奇偶性,最后输出相应的提示信息。注意需要分别对m为0、正数和负数的三种情况进行判断。