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");
            }
        }
    }
}

c sharp check if a number is odd or even

Explanation :

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

  1. Create one integer number n to read the user input.
  2. Ask the user to enter a number.
  3. Read the number and store it in_ n._
  4. Using an_ if-else_ statement, check if the number is divisible by 2 or not.
  5. If yes, print that the number is an even number.
  6. 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

c sharp check if a number is odd or even example

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.

Where is the color and why codevscolor ?

Long story short, I love paintings and I paint on weekends. We(me and my wife) have one Youtube channel. Don't forget to subscribe ❤️❤️🤩