Determine if a Number is Even or Odd in CSharp

Determine if a Number is Even or Odd in C#


Introduction of the Lesson

In the world of programming, numbers are not just for calculations—they also carry special properties that allow us to make decisions. One of the most common checks we perform is determining whether a number is even or odd.

An even number is divisible by 2 without a remainder (like 2, 4, 6, 10). An odd number is not divisible by 2 evenly (like 1, 3, 5, 11). This check is very useful in many real-world applications such as:

  • Deciding turn-taking in games (Player 1 moves on odd turns, Player 2 on even turns).
  • Checking whether a number fits a rule in mathematics.
  • Optimizing processes in computer science (e.g., splitting tasks or data).

In this lesson, you will learn how to use the modulus operator (%) in C# to check if a number is even or odd. By the end, you’ll gain practical skills in using arithmetic and relational operators together to create programs that respond logically to user input.


Objectives (OBE-Oriented)

To make learning clear and achievable, we focus on four outcomes: understand, learn, practice, and apply.

  • Understand: Explain what even and odd numbers are, and describe how the modulus operator (%) works in C#.
  • Learn: Write the correct syntax in C# to check whether a number is divisible by 2.
  • Practice: Develop and test a program in C# that determines whether a given number is even or odd.
  • Apply: Extend the concept to real-world style programs, such as checking ranges of numbers or validating multiple inputs for even/odd properties.

These objectives ensure a balance of theory, coding skills, and real-world application.


Beginner-Friendly Source Code & Instructions

Step-by-Step Instructions

  1. Open Visual Studio Code and ensure the .NET SDK is installed:
    dotnet --version
    
  2. Create a new console project:
    dotnet new console -n even_odd_checker
    cd even_odd_checker
    
  3. Open Program.cs and replace its content with the code below.
  4. Run the program:
    dotnet run
    

Program.cs

using System;

namespace EvenOddCheckerApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("=== Even or Odd Number Checker ===");

            // Step 1: Get user input
            Console.Write("Enter a number: ");
            int number = Convert.ToInt32(Console.ReadLine());

            // Step 2: Check if number is even or odd
            if (number % 2 == 0)
            {
                Console.WriteLine($"{number} is an EVEN number.");
            }
            else
            {
                Console.WriteLine($"{number} is an ODD number.");
            }
        }
    }
}

Example Outputs

Case 1 – Even number input

=== Even or Odd Number Checker ===
Enter a number: 12
12 is an EVEN number.

Case 2 – Odd number input

=== Even or Odd Number Checker ===
Enter a number: 7
7 is an ODD number.

Case 3 – Zero input

=== Even or Odd Number Checker ===
Enter a number: 0
0 is an EVEN number.

Explanation of the Program

  1. We start by importing the System namespace so we can use the Console class.
  2. Inside the Main method, the program prompts the user to input a number.
  3. We convert the input from a string to an integer using Convert.ToInt32().
  4. The modulus operator (%) is applied:
    • If number % 2 == 0, the number divides evenly by 2, so it’s even.
    • Otherwise, it’s odd.
  5. The result is displayed using Console.WriteLine().

This demonstrates how arithmetic operators (modulus) work hand-in-hand with relational operators (==) to evaluate conditions.


Summary

In this lesson, you learned how to check if a number is even or odd in C#. Using the modulus operator (%), you can determine whether a number has a remainder when divided by 2. This exercise introduced you to a practical application of arithmetic and relational operators, helping you better understand how logical decision-making works in programming. This simple concept is foundational for more complex tasks like loops, validations, and algorithm design.


5-Item Multiple Choice (Bloom’s Taxonomy)

  1. Which operator in C# is used to find the remainder of a division?
    • A. /
    • B. %
    • C. *
    • D. //
  2. What is the result of 9 % 2 in C#?
    • A. 0
    • B. 1
    • C. 2
    • D. 4
  3. If number % 2 == 0 is true, what can we conclude?
    • A. The number is odd
    • B. The number is even
    • C. The number is prime
    • D. The number is negative
  4. What will the program display if the user enters 0?
    • A. 0 is an ODD number
    • B. 0 is an EVEN number
    • C. Error
    • D. Nothing
  5. Why is checking for even or odd numbers important in programming?
    • A. To display text on the screen
    • B. To perform logical decisions and divide tasks
    • C. To convert data types
    • D. To calculate averages

Exercises, Assessment, and Lab Exam

Intro to Exercises

Now that you can check if a number is even or odd, it’s time to deepen your understanding with practice tasks. These exercises will reinforce the use of the modulus operator and decision-making structures in C#.


Exercises

  1. Modify the program to check if a number is positive, negative, or zero in addition to even/odd.
  2. Write a program that accepts three numbers and identifies how many are even and how many are odd.
  3. Extend the program to display whether a number is both even and greater than 100.
  4. Create a program that checks if a user’s age is even or odd.
  5. Write a program that asks for 5 numbers and displays which are even and which are odd (without using loops yet).

Assessment

Performance Task:
Write a program that:

  • Accepts one integer input.
  • Displays whether it is even or odd.
  • Includes additional logic to display if the number is also greater than 50.

Lab Exam

Instruction:
Create a C# console program that:

  1. Accepts a number from the user.
  2. Determines whether it is even or odd using the modulus operator.
  3. Displays the correct message.
  4. Displays “Invalid input” if the user enters a non-numeric value.

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

, , , , , , , , , ,

Post navigation