Program to check whether a number is Even or Odd in C#
This is one of the basic programming questions which is asked during the exams in colleges and in interviews. The logic behind the program is to check whether the number is divisible by 2 with remainder 0. If yes, then the input number is an example of an even number else it is an example of an odd number.
Below is the Program to check whether a number is even or odd in C#.
static void Main(string[] args)
{
//Variable to hold an input number
int inputNumber;
Console.Write("Enter a Number : ");
inputNumber = int.Parse(Console.ReadLine());
//If input number is divisible with 2 with the remainder 0
//then it is even number else odd number
if (inputNumber % 2 == 0)
{
Console.Write("{0} is an Even Number", inputNumber);
}
else
{
Console.Write("{0} is an Odd Number", inputNumber);
}
//Console.ReadLine() to hold the screen after the execution of the program
Console.ReadLine();
}
Preview:
I hope this program will help you in your interview/exam.