編寫一個 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP