Java: Is an Integer an Odd Number?

This is a pretty simple tip, but I still thought I would share it for anyone who is interested. To find out whether an integer is odd, you can use the modulo operator. This operator tells you the remainder after dividing the number by some other number. If you divide any integer by 2, you would expect a remainder of 1 if it is odd or no remainder if it is even.

public static boolean IsOdd(int number)
{
    return number % 2 == 1;
}

Leave a Reply