C# 判斷一個數是否能被2整除的程序

WBOY
發布: 2023-09-05 22:53:06
轉載
1266 人瀏覽過

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整除的範例。

現場示範< /p>

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
登入後複製

以上是C# 判斷一個數是否能被2整除的程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!