Check if a number is odd or even in C sharp

Published by admin on

C# check even or odd number

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.

Categories: c#

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *