Even or Odd Number Checker in C#
This C# program takes a user-inputted number and checks whether it is even or odd. It uses simple conditional logic to determine the result based on the number's divisibility by 2 and outputs the appropriate message to the user. This exercise introduces basic programming concepts like input handling, conditional statements, and modulo operations.
On this page
Source Code 💻
using System;
class Program
{
static void Main()
{
// Prompt user to enter a number
Console.Write("Enter an integer: ");
int number = Convert.ToInt32(Console.ReadLine());
// Check if the number is even or odd
if (number % 2 == 0)
{
Console.WriteLine($"{number} is an even number.");
}
else
{
Console.WriteLine($"{number} is an odd number.");
}
}
}
Explanation
- The user is prompted to input an integer.
- The program checks if the number is divisible by 2 using the modulus operator (%).
- If the remainder is 0, the number is even.
- If the remainder is not 0, the number is odd.
- Based on the result, it prints whether the number is even or odd.
🔚 end of document 🔚