Nếu phần dư của một số chia hết cho 2 là 0 thì nó sẽ chia hết cho 2.
Giả sử số của chúng ta là 5, chúng ta sẽ kiểm tra nó bằng cách sử dụng if-else sau -
// 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");
} Ví dụ
Sau đây là một ví dụ để tìm xem số đó có chia hết cho 2 hay không.
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();
}
}
} Đầu ra
Not divisible by 2