C# Program to check if a given number is Even or Odd.

Even numbers are those that are divisible by 2 without leaving a remainder, while odd numbers are not divisible by 2 without a remainder. In this article, we will explore how to write a simple C# program to check if a given number is even or odd.


Problem Statement: Write a C# program that takes an integer as input and determines whether it is an even or odd number.

Example:

Input: num = 7
Output: 7 is an Odd Number.

Explanation: 7 % 2 = 1

Input: num = 10
Output: 10 is an Even Number.

Explanation: 10 % 2 = 0

Steps to Check Even and Odd Numbers.

Below are the steps that you need to follow to check if the given number is odd or even.
Step 1: Start by taking an integer input from the user.
Step 2: Use the modulo operator % to check if the remainder of dividing the input number by 2 is zero or not.
Step 3: If the remainder is zero, it's an even number. Otherwise, it's an odd number.
Step 4: Print the appropriate message based on the result.

C# code to check Even and Odd Numbers.

// C-sharp code check if a number is even or odd
using System;

namespace EvenOddChecker
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter an integer: ");
            int number = Convert.ToInt32(Console.ReadLine());

            if (number % 2 == 0)
            {
                Console.WriteLine($"{number} is an Even number.");
            }
            else
            {
                Console.WriteLine($"{number} is an Odd number.");
            }
        }
    }
}
Output:
Enter an integer: 12
12 is an Even number.

Code Explanation:

We use the Console.Write and Console.ReadLine functions to take input from the user. The % operator calculates the remainder when the number is divided by 2. If the remainder is zero, the number is even; otherwise, it's odd.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS