How to find whether the Number is Divisible by 2 using C#?


To find whether a number is divisibe by 2 is not, check what happens when the number is divisible by 2.

If the remainder is 0, then the number is divisible by 2, else it is false −

if (num % 2 == 0) {
   Console.WriteLine("Divisible by 2 ");
} else {
   Console.WriteLine("Not divisible by 2");
}

The following is the complete code −

Example

 Live Demo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demo {
   class Test {
      static void Main(string[] args) {
         int num;
         num = 18;
         // 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

Divisible by 2

Updated on: 22-Jun-2020

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements