類似于判斷一個數(shù)字是偶數(shù)還是奇數(shù)一樣,要判斷一個給定的數(shù)是正數(shù)還是負數(shù)的程序也是非常簡單。 我們將在C語言中學習如何使用條件語句if-else來實現(xiàn)。
我們可以為下面給出的程序繪制一個流程圖 -

該算法的實現(xiàn)如下 -
#include <stdio.h>
int main() {
int number = -2;
if (number >= 0)
printf("%d is positive\n", number);
else
printf("%d is negative\n", number);
return 0;
}
執(zhí)行上面示例代碼,得到以下結(jié)果 -
-2 is negative