C# Program to Check if a Number is Divisible by 2
Posted by Superadmin on August 10 2022 14:25:54

C# Program to Check if a Number is Divisible by 2

 

This is a C# Program to find whether the number is divisible by 2.

Problem Description

This C# Program Finds whether the Number is Divisible by 2.

Problem Solution

Any whole number that ends in 0, 2, 4, 6, or 8 will be divisible by 2.Here the divisibility test is done by performing the mod function with 2.

Program/Source Code

Here is source code of the C# Program to Find whether the Number is Divisible by 2. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Find whether the Number is Divisible by 2
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication16
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;
            Console.WriteLine("Enter the Number :");
            n = int.Parse(Console.ReadLine());
            if (n % 2 == 0)
            {
                Console.WriteLine("Entered Number is Divisible by 2 ");
            }
            else
            {
                Console.WriteLine("Entered Number is Not Divisible by 2");
            }
            Console.ReadLine();
        }
    }
}
Program Explanation

In this C# program, we are reading the number using ‘n’ variable. If condition is used to check that the modulus of the value of ‘n’ variable by 2 is equal to 0. If the condition is true then execute the statement. Print the statement as the number is divisible by 2. Otherwise, if the condition is false, then execute the else statement and print the statement as not divisible by 2.

 
Runtime Test Cases
 
Enter the Number :
45
Entered Number is Not Divisible by 2