Java Program to Check Whether a Number Is Even or Odd

In this tutorial, we will learn how to write a Java program to check whether a number is even or odd. First, we will understand the basic concepts of even and odd numbers.

Next, we will write Java programs to check whether a number is even or odd using four different approaches:

  • Using a basic if-else statement with a hardcoded variable
  • Accepting input dynamically from the user using the Scanner class
  • Using a concise ternary operator (? 🙂
  • Using a user-defined method for modularity

What Are Even and Odd Numbers?

An even number is an integer that is exactly divisible by 2 without leaving any remainder. Examples include 2, 4, 6, 8, 10, and 0.

An odd number is an integer that is not exactly divisible by 2. Examples include 1, 3, 5, 7, 9, and -3.

Both positive and negative integers can be even or odd. For example, -8 is even, whereas -5 is odd.

The number zero is also even because dividing 0 by 2 leaves a remainder of 0.

How to Check Whether a Number Is Even or Odd in Java?

Java provides the modulus operator (%), which returns the remainder after division. We can determine if a number is even or odd using the following logic:

  • Even Number: If number % 2 == 0, the number is even.
  • Odd Number: Otherwise, the number is odd.

For example:

System.out.println(8 % 2);  // Output: 0 (Even)
System.out.println(7 % 2);  // Output: 1 (Odd)
System.out.println(-5 % 2); // Output: -1 (Odd)

In this example:

  • The number 8 is completely divisible by 2 with a remainder of 0, so it is even.
  • The number 7 leaves a remainder of 1, so it is odd.
  • Similarly, -5 leaves a remainder of -1 because Java’s modulus operator preserves the sign of the dividend, which means it is also odd.

Java Program to Check Whether a Number Is Even or Odd

Let us first write a simple Java program in which we assign an integer directly to a variable and use an if-else statement to determine whether it is even or odd.

Example Program 1: Using the if-else Statement

// Java program to check whether a hardcoded number is even or odd
public class EvenOdd {
    public static void main(String[] args) {
        // Declare and initialize the number to be checked
        int number = 15;

        // Use the modulus operator (%) to check if the number is divisible by 2
        if (number % 2 == 0) {
            // If the remainder is 0, the number is even
            System.out.println(number + " is an even number.");
        } else {
            // If the remainder is not 0 (it leaves 1 or -1), the number is odd
            System.out.println(number + " is an odd number.");
        }
    }
}

Expected Output:

15 is an odd number.

In this example program:

  • We declare an integer variable named number and assign it the value 15.
  • The condition number % 2 == 0 calculates the remainder when the number is divided by 2 and checks whether the remainder equals zero.
  • If the condition is true, the if block prints that the number is even.
  • If the condition is false, the else block prints that the number is odd.

In this example, 15 % 2 returns 1. Therefore, the condition is false, and the program displays that 15 is an odd number.

Java Program to Check Whether a Number is Even or Odd Using Scanner

In the previous example, we assigned a value directly to the variable. However, in practical applications, we often need to accept a number from the user. Therefore, Java provides the Scanner class in the java.util package to read input from the keyboard.

Example Program 2: Accepting Input from the User

// Java program to check a number is even or odd
// Import the Scanner class to read user input
import java.util.Scanner; 
public class EvenOddUsingScanner {
    public static void main(String[] args) {

        // Create a Scanner object to take input from the console
        Scanner sc = new Scanner(System.in);

        // Prompt the user to enter an integer
        System.out.print("Enter an integer: ");
        int number = sc.nextInt(); // Read the integer entered by the user

        // Check if the number is divisible by 2 using the modulus operator
        if (number % 2 == 0) {
            // If remainder is 0, it's even
            System.out.println(number + " is an even number.");
        } else {
            // Otherwise, it's odd
            System.out.println(number + " is an odd number.");
        }

        // Close the Scanner object to free system resources and prevent memory leaks
        sc.close();
    }
}

Expected Output:

Enter an integer: 24 
24 is an even number.

In this example program:

  • The statement import java.util.Scanner; imports the Scanner class.
  • The statement Scanner sc = new Scanner(System.in); creates an object of the Scanner class to read input from the user’s keyboard.
  • The Scanner class method sc.nextInt(); reads an integer entered by the user and stores it in a variable named number.
  • The if-else statement checks whether the remainder after division by 2 is zero.
  • Finally, sc.close(); closes the Scanner object.

This program can check any integer entered by the user within the range supported by Java’s int data type.

Java Program to Check Even or Odd Number Using the Ternary Operator

Java also provides the conditional operator (?:) to check whether a number is even or odd, which can be used as a concise alternative to an if-else statement.

Example Program 3: Using the Ternary Operator

// Java program to check whether a number is even or odd using the ternary operator
public class EvenOddTernary {
    public static void main(String[] args) {

        // Declare and initialize the number to be checked
        int number = 12;

        // Use the ternary operator (? :) as a concise shorthand for an if-else statement
        // Syntax: condition ? expression_if_true : expression_if_false
        String result = (number % 2 == 0)
                ? "Even" // Assigned if the condition is true
                : "Odd";  // Assigned if the condition is false

        // Print the final output combining the number and the result string
        System.out.println(number + " is " + result + ".");
    }
}

Expected Output:

12 is Even.

In this example, the expression number % 2 == 0 checks whether the number is even.

  • If the condition is true, the ternary operator returns “Even”.
  • If the condition is false, it returns “Odd”.

The resulting string is stored in the result variable and displayed on the console.

For this simple problem, both the if-else statement and the ternary operator work correctly. Beginners should start with if-else because it makes the decision-making process easier to understand.

Java Program to Check Even or Odd Number Using a Method

We can also place the checking logic inside a separate user-defined method. This approach is useful when the same logic needs to be reused in different parts of a program.

Example Program 4: Using a User-Defined Method

// Java program to check whether a number is even or odd using a user-defined method

public class EvenOddMethod {

    // User-defined static method to check if a number is even
    // Returns true if the number is even, and false if it is odd
    static boolean isEven(int number) {
        return number % 2 == 0;
    }

    public static void main(String[] args) {
        // Declare and initialize the number to be checked (testing with a negative number)
        int number = -8;

        // Call the user-defined isEven() method inside the if condition
        if (isEven(number)) {
            // Executed if isEven() returns true
            System.out.println(number + " is an even number.");
        } else {
            // Executed if isEven() returns false
            System.out.println(number + " is an odd number.");
        }
    }
}

Expected Output:

-8 is an even number.

In this example, the static method isEven() accepts an integer and returns a boolean value.

  • It returns true when the number is divisible by 2 without a remainder.
  • It returns false when the number is odd.

The main() method calls isEven(number) and uses the returned value to display the result. This method can be reused with different integer values without repeating the checking logic.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.