How to Test for Even or Odd Number in C#?
To check whether a number is even or odd in C#, we can use the modulo operator over 2 on that number, if result is 0 then the number is even otherwise it's an odd number.Example
using System;
public class Example
{
public static void Main()
{
int num = 7;
if(num % 2 == 0){
Console.WriteLine("Number is even");
} else {
Console.WriteLine("Number is odd");
}
}
}
Output
Number is odd