在下面的程序中,我们检查输入整数是正数还是负数。如果输入数字大于零,则其为正数,否则为负数。如果数字为零则既不是正数也不是负数。我们在下面的 C 程序中遵循了相同的逻辑。
/* Description: A program to check whether the input
* integer number is positive or negative.
* Written by: Chaitanya Singh
* Published on: beginnersbook.com
*/
#include <stdio.h>
void main()
{
int num;
printf("Enter a number: \n");
scanf("%d", &num);
if (num > 0)
printf("%d is a positive number \n", num);
else if (num < 0)
printf("%d is a negative number \n", num);
else
printf("0 is neither positive nor negative");
}
输出 1:
Enter a number:
0
0 is neither positive nor negative
输出 2:
Enter a number:
-3
-3 is a negative number
输出 3:
Enter a number:
100
100 is a positive number