C# - Read Number and Check ODD or EVEN
I have given here a C# program to read number and check ODD or EVEN. It uses mod (%) operator with 2. If the remainder is 1, then it would be an ODD number otherwise EVEN Number.
Source Code
using System;
using System.Collections.Generic;
using System.Text;
namespace OddEven
{
class OddOREven
{
static bool ReadInteger(out int n)
{
string input = System.Console.ReadLine();
n = 0;
try
{
n = Convert.ToInt32(input);
return true;
}
catch (System.Exception ex)
{
System.Console.WriteLine("Error in the input format\n\n");
return false;
}
}
static void Main(string[] args)
{
System.Console.Write("Enter a Number to Check ODD or EVEN: ");
int number = 0;
ReadInteger(out number);
if((number % 2) == 0)
System.Console.WriteLine("{0} is an EVEN number", number);
else
System.Console.WriteLine("{0} is an ODD number", number);
}
}
}
Output
Enter a Number to Check ODD or EVEN: 15
15 is an ODD number
Press any key to continue . . .
Enter a Number to Check ODD or EVEN: 22
22 is an EVEN number
Press any key to continue . . .
|