How to check if a number is odd or even?

Advertisement

More C# Resources

The best way to see if a given number is odd or even is to use the modulus operator (%). The modulus operator returns the remainder when dividing two numbers. So if you do a modulus of 6 % 2 you get 0 because 6 divided by 2 doesn’t have a remainder, therefore it’s even. If you do 3 % 2 the remainder will be 1, therefore the number is odd.

int Num = 5;
if(Num % 2 == 0)
{
// It's even
}
else
{
// It's odd
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top