# Check if a number is odd or even in C sharp

# How to check if a number is even or odd in C# :

In this tutorial, we will learn how to check if a number is even or odd using* c sharp*. The program will take the number as input from the user and print out the result i.e. the* number is even or odd.*

With this example program, you will learn* how to print anything on console, how to read user input in C#, how to use if-else conditions* and* how to use modulus operator in C#*.

## % Operator in C# :

*% operator* computes the *remainder* after dividing one number by the another.* a%b* will return the remainder after dividing *a by b*. For example :

Console.WriteLine(5 % 4); Console.WriteLine(8 % 4); Console.WriteLine(10 % 4); Console.WriteLine(10 % -9); Console.WriteLine(-10 % 9); Console.WriteLine(-10 % -9);

The above program will print the below output :

1 0 2 1 -1 -1

As you can see the sign of the result is the same as the first operand.

This example is dealing with only integers but we can also use % with floating point numbers. For floating point numbers, the result will be a float value.

## Finding out if a number is even or odd :

A number is called an *even *number if it is divisible by *2*. Otherwise, it is an *odd* number. We will use the *%* operator to find out if a number is *even* or *odd*. If for a number *n, n%2 is 0 *if it is an *even* number, else it is an *odd* number.

### C# Program :

using System; namespace dotnet_sample { class Program { static void Main(string[] args) { //1 int n; //2 Console.WriteLine("Enter a number to check : "); //3 n = int.Parse(Console.ReadLine()); //4 if(n % 2 == 0) { //5 Console.WriteLine(n + " is an even number"); } else { //6 Console.WriteLine(n + " is an odd number"); } } } }

### Explanation :

*The commented numbers in the above program denote the step numbers below :*

- Create one integer number
*n*to read the user input. - Ask the user to enter a number.
- Read the number and store it in
*n.* - Using an
*if-else*statement, check if the number is divisible by*2*or not. - If yes, print that the number is an
*even*number. - Else, print that the number is an
*odd*number.

### Sample Output :

Enter a number to check : 10 10 is an even number Enter a number to check : 12 12 is an even number Enter a number to check : 13 13 is an odd number Enter a number to check : 12543 12543 is an odd number

*This program is available on Github.*

### Conclusion :

We have learned how to check if a number is even or not in c#. Try to run the program and drop one comment below if you have any queries.

## 0 Comments