Here this post provides details about odd numbers and even numbers also provides program in C# to check even odd number.
Odd Numbers
Odd numbers are integers that cannot be evenly divided by 2. When you divide an odd number by 2, you get a remainder of 1.
Characteristics of Odd Numbers:
- They always end with 1, 3, 5, 7, or 9 in decimal notation.
- Examples include: -5, -3, -1, 1, 3, 5, 7, 9, 11, etc.
Properties:
- The sum of two odd numbers is always even.
- The sum of an odd number and an even number is always odd.
- The product of two odd numbers is always odd.
Program to check Odd Numbers:
Here's a simple C# program to check whether a given number is odd. This program prompts the user for input and displays the result accordingly.
using System;
class OddChecker
{
static void Main()
{
Console.Write("Enter a number: ");
// Try to read and convert the input to an integer
try
{
int number = Convert.ToInt32(Console.ReadLine());
if(number%2==0)
Console.WriteLine($"The provided number {number} is not Odd.");
else
Console.WriteLine($"The provided number {number} is Odd.");
}
catch (FormatException)
{
Console.WriteLine("Please enter a valid integer.");
}
}
}
Even Numbers
Even numbers are integers that can be evenly divided by 2, meaning there is no remainder when divided by 2.
Characteristics of Even Numbers:
- They always end with 0, 2, 4, 6, or 8 in decimal notation.
- Examples include: -4, -2, 0, 2, 4, 6, 8, 10, etc.
Properties:
- The sum of two even numbers is always even.
- The sum of an even number and an odd number is always odd.
- The product of two even numbers is always even.
Program to check Even Numbers:
Here's a simple C# program to check whether a given number is Even. This program prompts the user for input and displays the result accordingly.
using System;
class OddChecker
{
static void Main()
{
Console.Write("Enter a number: ");
// Try to read and convert the input to an integer
try
{
int number = Convert.ToInt32(Console.ReadLine());
if(number%2==0)
Console.WriteLine($"The provided number {number} is Even.");
else
Console.WriteLine($"The provided number {number} is not Even.");
}
catch (FormatException)
{
Console.WriteLine("Please enter a valid integer.");
}
}
}
How the Programs Works:
- The program uses a
try-catch
block to handle any invalid inputs. - Then checks if the number is divisible by 2.
- The result is displayed to the user.
How to Run the Program:
- Open your C# development environment (like Visual Studio or Visual Studio Code).
- Create a new Console Application.
- Copy and paste the code into the
Main
class. - Run the application. You will be prompted to enter a number.
- Enter a number, and the program will tell you if it's even or odd.
Thanks
Tags
csharp