C# 程式查詢數字是否可以被 2 整除


如果數字除以 2 的餘數為 0,那麼就可以被 2 整除。

假設我們的數字是 5,我們將使用以下 if-else 進行檢查 -

// checking if the number is divisible by 2 or not
if (num % 2 == 0) {
   Console.WriteLine("Divisible by 2 ");
} else {
   Console.WriteLine("Not divisible by 2");
}

示例

以下是一個示例,可以用來判斷數字是否可以被 2 整除。

線上演示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
   class MyApplication {
      static void Main(string[] args) {
         int num;
         num = 5;
         // checking if the number is divisible by 2 or not
         if (num % 2 == 0) {
            Console.WriteLine("Divisible by 2 ");
         } else {
            Console.WriteLine("Not divisible by 2");
         }
         Console.ReadLine();
      }
   }
}

輸出

Not divisible by 2

更新日期:2020 年 6 月 19 日

6K+ 瀏覽量

開啟你的 職業生涯

透過完成此課程獲得認證

開始
廣告
© . All rights reserved.