>  기사  >  백엔드 개발  >  숫자가 2로 나누어지는지 확인하는 C# 프로그램

숫자가 2로 나누어지는지 확인하는 C# 프로그램

WBOY
WBOY앞으로
2023-09-05 22:53:061263검색

C# 判断一个数是否能被2整除的程序

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");
}

Example

다음은 숫자가 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

위 내용은 숫자가 2로 나누어지는지 확인하는 C# 프로그램의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제