Program to Check Even Number and Odd Number

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:

  1. Open your C# development environment (like Visual Studio or Visual Studio Code).
  2. Create a new Console Application.
  3. Copy and paste the code into the Main class.
  4. Run the application. You will be prompted to enter a number.
  5. Enter a number, and the program will tell you if it's even or odd.

Thanks

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال