編寫一個 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
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP