숫자가 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