Home >Backend Development >C#.Net Tutorial >C# program to determine whether a number is divisible by 2

C# program to determine whether a number is divisible by 2

WBOY
WBOYforward
2023-09-05 22:53:061393browse

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

If the remainder of dividing this number by 2 is 0, then it is divisible by 2.

Suppose our number is 5, we will use the following if-else to check it -

// 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

Here is an example to determine if a number is divisible by 2 .

Live Demo

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

Output

Not divisible by 2

The above is the detailed content of C# program to determine whether a number is divisible by 2. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete