---
title: Check Whether a Number is Even or Odd [if-else & ternary]
url: https://docs.vultr.com/java/examples/check-whether-a-number-is-even-or-odd-if-else--ternary
publish_date: 2024-09-27T17:11:22.269984Z
last_updated: 2024-09-27T17:11:22.269984Z
header_image: https://assets.vultrdevrel.com/docs-main-doc-assets-1/collection-items/391/d474f4ee-6a11-4643-8a29-bf59f3659df7.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Java Program to Check Whether a Number is Even or Odd [if-else & ternary]

## Introduction

Determining whether a number is even or odd is a fundamental concept in programming, often introduced as one of the first conditional logic exercises for beginners. In Java, this can be efficiently achieved using `if-else` statements or the ternary operator. Both methods provide clear ways to implement conditional logic based on the modulus operation, primarily used to check the divisibility of numbers.

In this article, you will learn how to implement a Java program to check if a number is even or odd using both `if-else` statements and the ternary operator. Explore example implementations to master these conditional structures whilst also learning some best practices for clean and efficient Java code.

## Checking Even or Odd Using if-else

### Basic if-else Implementation 
1. Define a method that takes an integer as its parameter.
1. Use the `if-else` statement to check if the integer is divisible by 2 without leaving a remainder.
1. Print the result based on the condition.

    ```java
    public class EvenOrOdd {
        public static void checkEvenOdd(int number) {
            if (number % 2 == 0) {
                System.out.println(number + " is even.");
            } else {
                System.out.println(number + " is odd.");
            }
        }

        public static void main(String[] args) {
            checkEvenOdd(10);  // Output: 10 is even.
            checkEvenOdd(15);  // Output: 15 is odd.
        }
    }
    ```

    This segment initializes the method `checkEvenOdd` which evaluates if `number % 2 equals 0`. If true, it prints that the number is even; otherwise, it reports the number as odd.

## Utilizing the Ternary Operator

### Ternary Operation to Evaluate Even or Odd
1. Introduce a method that employs the ternary operator for the same divisibility test.
1. The ternary operator provides a shorthand method for the `if-else` statement and is useful for simple condition checks.

    ```java
    public class EvenOrOdd {
        public static void checkEvenOddTernary(int number) {
            String result = (number % 2 == 0) ? number + " is even." : number + " is odd.";
            System.out.println(result);
        }

        public static void main(String[] args) {
            checkEvenOddTernary(10);  // Output: 10 is even.
            checkEvenOddTernary(25);  // Output: 25 is odd.
        }
    }
    ```

    In this implementation, the method `checkEvenOddTernary` uses the ternary operator to streamline the conditional logic. The condition `(number % 2 == 0)` is evaluated, and the corresponding string for even or odd is printed based on the result.

## Conclusion

Java offers multiple ways to implement conditional checks, with `if-else` and the ternary operator being among the most commonly used for simple decisions like checking if a number is even or odd. The examples demonstrated provide two straightforward methods to achieve this check effectively. By understanding both approaches, you can select the most appropriate method depending on the complexity of the condition and the need for readability in your code. Employ these practices to enhance your Java programming skills and make your code both efficient and easy to understand.