編寫一個 C# 程式來檢查數字是否能被 2 整除


要檢查數字是否可以被2整除,您需要先查詢餘數。

如果數字除以2後的餘數為0,則它將可以被2整除。

假設我們的數字是10,我們將使用以下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 = 10;

         // 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();
      }
   }
}

輸出

Divisible by 2

更新時間: 2020年6月20日

2K+ 次瀏覽

啟動你的 職業生涯

完成課程取得認證

開始學習
廣告
© . All rights reserved.