if语句编写第一个程序: 程序要求:输入一个整数,如果大于等于0则输出 is positive,否则输出is negative。 # include<stdio.h> main()
时间: 2024-10-24 14:13:47 浏览: 27
好的,你可以使用if语句根据条件来判断输入的整数并输出相应的消息。下面是符合你需求的C语言程序示例:
```c
#include <stdio.h>
int main() {
int input;
// 输入一个整数
printf("Enter an integer: ");
scanf("%d", &input);
// 使用if语句进行判断
if (input >= 0) {
printf("The number is positive.\n");
} else {
printf("The number is negative.\n");
}
return 0;
}
```
在这个程序中,我们首先定义了一个变量`input`用于存储用户的输入。然后使用`scanf`从标准输入读取一个整数。接着,`if`语句检查`input`是否大于或等于0,如果是,则打印"positive";否则,打印"negative"。
现在,如果你运行这个程序,按照提示输入一个整数,程序会根据输入的结果给出相应提示。
相关问题
编写C语言程序,从键盘读入一个正整数n,如果整数n大于零,则输出positive以及是几位数,如果整数n小于零,则输出negative以及绝对值是几位数,如果整数n等于零,则zero。
在C语言中,你可以使用`scanf()`函数从键盘读取整数,然后根据条件判断输出相应的信息。以下是一个简单的示例程序:
```c
#include <stdio.h> // 包含标准输入输出库
int main() {
int n;
// 从键盘读取整数
printf("Enter an integer: ");
scanf("%d", &n);
if (n > 0) {
printf("The number is positive and has %d digits.\n", floor(log10(n) + 1));
} else if (n < 0) {
printf("The number is negative, its absolute value is %d digits.\n", floor(log10(-n) + 1));
} else {
printf("The number is zero.\n");
}
return 0; // 主函数结束标志
}
```
在这个程序中,我们首先读取用户的输入并存储在变量`n`中。然后用一系列`if...else if...else`语句检查`n`的值:
- 如果`n`大于0,我们计算它的位数(不包括前导零)使用`log10(n) + 1`(因为`log10`返回的是以10为底的对数,我们需要加1得到实际的位数),然后向下取整(`floor()`函数)以确保结果总是整数。
- 如果`n`小于0,我们先取它的绝对值(`-n`),再计算位数。
- 最后,如果`n`等于0,我们直接输出"zero"。
编程判断输入整数的正负性和奇偶性。 **输入格式要求:"%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;
}
```
程序使用了if语句进行条件判断,首先判断m是否等于0,如果是则输出"0 is zero. It is an even"。否则,再分别判断m的正负性和奇偶性。如果m为正偶数,输出"%d is a positive even",如果m为正奇数,输出"%d is a positive odd"。如果m为负偶数,输出"%d is a negative even",如果m为负奇数,输出"%d is a negative odd"。
阅读全文
相关推荐
















