![Check Whether a Number is Even or Odd [if-else & ternary] header image](https://sjc1.vultrobjects.com/docs-main-doc-assets-1/collection-items/391/d474f4ee-6a11-4643-8a29-bf59f3659df7.webp)
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
Define a method that takes an integer as its parameter.
Use the
if-else
statement to check if the integer is divisible by 2 without leaving a remainder.Print the result based on the condition.
javapublic 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 ifnumber % 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
Introduce a method that employs the ternary operator for the same divisibility test.
The ternary operator provides a shorthand method for the
if-else
statement and is useful for simple condition checks.javapublic 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.
No comments yet.