Java Program To Check Even or Odd Numbers

Determining whether a number is even or odd is a fundamental logic puzzle that every programmer encounters early in their journey. In mathematical terms, an even number is any integer that can be divided by two without leaving a remainder. If there is a remainder of one, the number is odd.

In Java, we handle this logic using the Modulus Operator (%), which calculates the remainder of a division operation. Below is a clean, beginner-friendly implementation of an even/odd checker.

import java.util.Scanner;

public class EvenOddChecker {
    public static void main(String[] args) {
        // Create a Scanner object to read input from the user
        Scanner scanner = new Scanner(System.in);

        // Prompt the user to enter a number
        System.out.print("Enter an integer to check: ");
        
        // Check if the input is actually an integer
        if (scanner.hasNextInt()) {
            int number = scanner.nextInt();

            // Check if the number is even or odd using the modulus operator
            if (number % 2 == 0) {
                System.out.println(number + " is an even number.");
            } else {
                System.out.println(number + " is an odd number.");
            }
        } else {
            System.out.println("Invalid input. Please enter a whole number.");
        }

        // Close the Scanner object to prevent resource leaks
        scanner.close();
    }
}
Developer Tip: While the modulus operator is the standard way to check for parity, advanced developers sometimes use bitwise operators for performance. For example, (number & 1) == 0 is a very fast way to check if a number is even at the hardware level.

How the Program Works

Let's break down the core components of this code so you understand exactly what is happening under the hood:

  • The Scanner Class: We use import java.util.Scanner; to bring in a utility that allows our program to "listen" to what the user types in the console.
  • Reading Input: The method scanner.nextInt() pauses the program and waits for the user to type a number and press Enter. That value is then stored in the number variable.
  • The Modulus Logic: This is the most important part. The expression number % 2 divides the number by 2 and returns only the remainder.
    • If 10 % 2 is calculated, the result is 0 (Even).
    • If 11 % 2 is calculated, the result is 1 (Odd).
  • The If-Else Statement: This acts as a fork in the road. If the condition (number % 2 == 0) is true, the first block of code runs. If it is false, the else block runs.
  • Resource Management: We call scanner.close() at the end. In Java, objects that handle "streams" (like user input) should be closed when finished to free up system memory.
Common Mistake: Beginners often confuse the assignment operator (=) with the comparison operator (==). Always use == when checking if two values are equal in an if statement.

Real-World Applications

Checking for even or odd numbers isn't just for math homework. Developers use this logic in various real-world scenarios:

  • UI Design: Creating "Zebra Stripes" in a table. You check if a row index is even or odd to apply different background colors, making the data easier to read.
  • Load Balancing: Distributing tasks across two different servers by sending even-numbered tasks to Server A and odd-numbered tasks to Server B.
  • Game Development: Alternating turns between Player 1 and Player 2 in a board game.
Best Practice: When dealing with user input, always validate that the input is the correct type. Using scanner.hasNextInt() prevents your program from crashing if a user accidentally types a letter instead of a number.
Watch Out: In Java, the modulus of a negative odd number is -1, not 1. While number % 2 == 0 will still correctly identify negative even numbers, be careful if you ever try to check for odd numbers using number % 2 == 1, as it will fail for negative inputs!