In this article, we’ll explore how to write a C# program to check if a Given Number is Even or Odd.
Table of Contents
Prerequisites
Before diving into the code, let’s ensure we clearly understand even and odd numbers.
- Even Numbers:Â The numbers divisible by 2 without leaving a remainder are known as Even numbers. For example, 2, 4, 6, 8, and 10 are even numbers because each of them can be divided by 2 without leaving any remainder.
- Odd Numbers:Â On the other hand, odd numbers are integers that are not divisible by 2, and they leave a remainder of 1 when divided by 2. Examples of odd numbers include 1, 3, 5, 7, and 9. When you try to divide these numbers by 2, there is always a remainder of 1.
Approach 1: Using Modulo Operator
The modulo operator (%) helps us determine the remainder when dividing one number by another. The number is even if the remainder is zero; otherwise, it’s odd.
using System;
class Program
{
static void Main()
{
// Get user input
Console.Write("Enter a number: ");
int num = Convert.ToInt32(Console.ReadLine());
// Check if the number is even or odd
if (num % 2 == 0)
{
Console.WriteLine($"{num} is an even number.");
}
else
{
Console.WriteLine($"{num} is an odd number.");
}
}
}
Output:
Enter a number: 5
5 is an odd number.
Code Explanation:
- We use
Console.ReadLine()
to take user input. - The
%
operator calculates the remainder of the division. - The result of
num % 2
is checked to determine whether it’s equal to 0 (even) or not (odd).
Approach 2: Bitwise AND Operator
Using bitwise operations is another efficient way to check for even or odd numbers.
using System;
class Program
{
static void Main()
{
// Get user input
Console.Write("Enter a number: ");
int num = Convert.ToInt32(Console.ReadLine());
// Check if the number is even or odd
if ((num & 1) == 0)
{
Console.WriteLine($"{num} is an even number.");
}
else
{
Console.WriteLine($"{num} is an odd number.");
}
}
}
Output:
Enter a number: 2
2 is an even number.
Code Explanation:
- The bitwise
AND
operation with 1 (num & 1
) isolates the least significant bit. - If the result is 0, the number is even.
C# program to print odd and even numbers from 1 to 10
Here is a C# program to print odd and even numbers from 1 to 10.
using System;
class Program
{
static void Main()
{
Console.WriteLine("Even and Odd numbers from 1 to 10:");
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
Console.WriteLine($"{i} is even, ");
}
else
{
Console.WriteLine($"{i} is odd, ");
}
}
}
}
Output:
Even and Odd numbers from 1 to 10:
1 is odd,
2 is even,
3 is odd,
4 is even,
5 is odd,
6 is even,
7 is odd,
8 is even,
9 is odd,
10 is even,
Code Explanation:
- We use a
for
loop to iterate from 1 to 10. - The
if
statement checks if the current numberi
is even using the modulo%
operator. If true, it prints that the number is even; otherwise, it prints that the number is odd. - The
Console.Write
statement is used to print the result
Conclusion
In this article, we choose the modulo operator and bitwise operations to Check if a Given Number is Even or Odd in C#. Both methods achieve the same goal.
Feel free to experiment with these approaches and incorporate them into your C# programming. Happy coding!
Recommended Articles:
- How to remove duplicate characters from a String in C#
- C# program to count the occurrences of each character in a String
- C# program to count vowels and consonants in a string
- Leap Year Program in C, C++, C#, JAVA, PYTHON, and PHP
- Program to print prime numbers from 1 to N
- Bubble sort programs in C, C++, JAVA, and PYTHON
- Fibonacci sequence: Fibonacci series in C# (with examples)
- Program to copy all elements of an array into another array
- Different Ways to Calculate Factorial in C# (with Full Code Examples)
- Program to Convert Fahrenheit to Celsius: Algorithm, Formula, and Code Examples
- Converting Celsius to Fahrenheit in C#
- Different Ways to Calculate Factorial in C# (with Full Code Examples)
- C# Tutorial
- Difference Between Array And ArrayList In C#: Choosing the Right Collection - May 28, 2024
- C# Program to Capitalize the First Character of Each Word in a String - February 21, 2024
- C# Program to Find the Longest Word in a String - February 19, 2024