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
Release: 2023-09-05 22:53:06
forward
1341 people have browsed it

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

Example

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

Live Demo< /p>

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

Output

Not divisible by 2
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template