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로 나누어 떨어지는지 여부를 확인하는 예입니다.
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