- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
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 thenumbervariable. - The Modulus Logic: This is the most important part. The expression
number % 2divides the number by 2 and returns only the remainder.- If
10 % 2is calculated, the result is0(Even). - If
11 % 2is calculated, the result is1(Odd).
- If
- 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, theelseblock 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!