C 程式設計檢查一個數字是正數、負數還是 0?


大於 0 的數字為正數,小於 0 的數字為負數。正負概念在數論和程式設計中非常重要。計算僅依賴於此概念。

Input: 0
Output:0 is zero

說明

使用條件語句檢查數字與 0 的關係,是大於 0 還是小於 0。

示例

#include <iostream>
using namespace std;
int main() {
   int n=0;
   if(n>0) {
      printf("%d is positive",n);
   } else if(n<0) {
      printf("%d is negative",n);
   } else {
      printf("%d is zero",n);
   }
   return 0;
}

更新於:19-Aug-2019

882 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告