Example program to check if a number is even or odd in C# using reminder of the number divided by 2 and if else condition.

Brief of even and odd number – The number which is completely divisible by 2, means, its remainder is 0, then it is known as even number and when remainder is not 0, then it is an odd number.

For example, if we divide the number 2,4,6,8…etc. by 2, then remainder will always come zero.

In the C# program code, we will use modulus operator to calculate the remainder.

We can also use bitwise(&),division(/) and multiplication(*),( >>,<<) Right shift and Left Shift operators,minus(-) operators to calculate the number is even or odd.

Code to check even or odd in C#

Method-1 – using % operator

We will calculate the remainder using modulus operator (%). For  e.g. num%2==remainder. we  will check if the remainder is 0 in if else condition. If remainder is zero, we will print even number or else print odd number.

if(number%2==0)
{
Even Number
}
else
{
odd number
}

Code:

using System;
    class EvenOrOdd
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please Enter a Possitive Number");
          bool flag = false;
          int number;
            flag = int.TryParse(Console.ReadLine(),out number);
            if(flag==false)
            {
                Console.WriteLine("Invalid Number");
                return;
            }
            if(number<0)
            {
                Console.WriteLine("Number is negative");
                return;
            }

            if(number%2==0)
            {
                Console.WriteLine("Even Number");
            }
            else
            {
                Console.WriteLine("Odd Number");
            }
        }
    }

Method-2 – using bitwise & operator

& operator -performs bitwise multiplication operation.

& value bit sets to 1 only if both bits are 1 otherwise 0.

number=number&1
if(number==0)
{
Even Number
}
else
{
Odd Number
}

Code:

class EvenOrOdd
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please Enter a Possitive Number");
             bool flag = false; 
             int number;
            flag = int.TryParse(Console.ReadLine(),out number);
            if(flag==false)
            {
                Console.WriteLine("Invalid Number");
                return;
            }
            if(number<0)
            {
                Console.WriteLine("Number is negative");
                return;
            }
            
            if ((number & 1) == 0)
            {
                Console.WriteLine("Even Number");
            }
            else
            {
                Console.WriteLine("Odd Number");
            }
        }
    }

Method-3 using /, * operators


/ – operator performs division operation
* – operator performs multiplication operation

k=(number/2)*2
if(number==k)
{
Even Number
}
else
{
Odd Number
}

Code

class EvenOrOdd
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please Enter a Possitive Number");
            bool flag = false;
            int number;
            flag = int.TryParse(Console.ReadLine(),out number);
            if(flag==false)
            {
                Console.WriteLine("Invalid Number");
                return;
            }
            if(number<0)
            {
                Console.WriteLine("Number is negative");
                return;
            }
            
            if ((number /2)*2 == number)
            {
                Console.WriteLine("Even Number");
            }
            else
            {
                Console.WriteLine("Odd Number");
            }
        }
    }

Method-4 using shift operators

(>>–Right shift,<> – Right shift operation performs division of a number with 2.
<<– left shift operation performs multiplication of a number with 2.

K=(Number>>1)<<1
if(k==Number)
{
Even Number
}
else
{
Odd Number
}

Code

class EvenOrOdd
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please Enter a Possitive Number");
            bool flag = false;
            int number;
            flag = int.TryParse(Console.ReadLine(),out number);
            if(flag==false)
            {
                Console.WriteLine("Invalid Number");
                return;
            }
            if(number>1)<<1 == number)
            {
                Console.WriteLine("Even Number");
            }
            else
            {
                Console.WriteLine("Odd Number");
            }
        }
    }


Method -5 using – operator.

Loop
number=number-2;
if(number==0)
{
even
return;
}
if(number==1)
{
odd
return;
}
End loop;

Code

class EvenOrOdd
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please Enter a Possitive Number");

            bool flag = false;
            int number;
            flag = int.TryParse(Console.ReadLine(),out number);
            if(flag==false)
            {
                Console.WriteLine("Invalid Number");
                return;
            }
            if(number1)
            {
                number = number - 2;

                if(number==0 || number==1)
                {
                    if (number == 0)
                    {
                        Console.WriteLine("Even Number");
                        return;
                    }

                    if (number == 1)
                    {
                        Console.WriteLine("Odd Number");
                         return;

                    }
                }
            }
        }
    }

Related Posts