C# Program to Check if a number is even or odd

Logic to check if a number is odd or even

C# programs to check if a number is even or odd have been shown here. An integer number $n$ is said to be even if it is divisible by $2$ i.e. $(n\mod 2 = 0)$ and it is called odd if it is not divisible by 2 i.e. it does not result in 0 as the remainder when divided by $2$. So a number in the form of $2n$, where $n$ is an integer, is called even and a number in the form of $2n+1$ or $2n-1$ is called odd. For example $... -4, -2, 0, 2, 4$ ... etc. are even numbers and $... -3, -1, 1, 3, 5$ ... etc. are odd numbers.

  Another approach is to apply bitwise AND operator (&) to check if $n$ is even or odd. We need to perform bitwise AND operation between $n$ and $1$. If $n$ AND $1 = 0$, then $n$ is even and if $n$ AND $1 = 1$, then $n$ is odd.






1. Algorithm to check if a number is even or odd


// $n$ is an integer number as input//


1. Check if $n \mod 2 = 0$, then Return Even else

2. Return Odd





2. Pseudocode to check if a number is even or odd


Input: An integer number $n$

Output: If $n$ is even or odd

1. Procedure evenOrOdd($n$):

2. If $n\mod2==0$:

3. Return even

4. Else:

5. Return odd

6. End Procedure





3. Time complexity to check if a number is even or odd


Time Complexity: O(1)





4. C# Program to check if a number is even or odd

Code has been copied
/**********************************************
            alphabetacoder.com
C# program to check if a number is even or odd 
***********************************************/

using System;

namespace EvenOdd {
    class Program {
        static void Main(string[] args) {
            // declare variable
            int n;

            //take input
            Console.Write("Enter the number = ");
            n = Convert.ToInt32(Console.ReadLine());

            //check if n is even or odd
            if (n % 2 == 0)
                Console.WriteLine(n + " is even");
            else
                Console.WriteLine(n + " is odd");

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Case 1:

Enter the number = -5

-5 is odd


Case 2:

Enter the number = 8

8 is even


Case 3:

Enter the number = 3

3 is odd




5. C# Program to check if a number is even or odd using bitwise AND operation

Code has been copied
/***************************************
        alphabetacoder.com
 C# program to check if a number is 
 even or odd using bitwise AND operation
****************************************/

using System;

namespace EvenOdd {
    class Program {
        static void Main(string[] args) {
            // declare variable
            int n;

            //take input
            Console.Write("Enter the number = ");
            n = Convert.ToInt32(Console.ReadLine());

            //check if n is even or odd
            // by doing bitwise AND operation
            // with 1. If the result is 0 then
            // n is even. If the result is 1 
            // then n is odd.
            if ((n & 1) == 0)
                Console.WriteLine(n + " is even");
            else
                Console.WriteLine(n + " is odd");

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Case 1:

Enter the number = 15

15 is odd


Case 2:

Enter the number = 22

22 is even


Case 3:

Enter the number = 13

13 is odd