Full Stack Development

Java Program to Check If a Number is Even or Odd Full Guide

Arnav Arnav
Dec 28, 2024 3 Min Read
4Achievers Blogs Custom Gem 4Achievers Blogs said
Java Programming Basics

Java Program to Check Even or Odd

A comprehensive guide for beginners to understand the logic, flow, and multiple ways to solve the "Even or Odd" problem in Java.

The Logic Behind Even & Odd

In mathematics, an even number is an integer that is exactly divisible by 2. When you divide an even number by 2, the remainder is always 0. Conversely, an odd number leaves a remainder of 1.

The Modulo Operator (%)

In Java, we use the % (modulo) operator to find the remainder of a division.
Example: 10 % 2 results in 0 (Even), while 11 % 2 results in 1 (Odd).

Method 1: Using If-Else Statement

This is the most common and readable way for beginners to write the program.

import java.util.Scanner;

public class EvenOdd {
public static void main(String[] args) {
// Create Scanner object to read input
Scanner reader = new Scanner(System.in);

    System.out.print(<span class="string">"Enter a number: "</span>);
    <span class="keyword">int</span> num = reader.nextInt();

    <span class="keyword">if</span> (num % <span class="string">2</span> == <span class="string">0</span>) {
        System.out.println(num + <span class="string">" is Even"</span>);
    } <span class="keyword">else</span> {
        System.out.println(num + <span class="string">" is Odd"</span>);
    }
}
}

Method 2: Using Ternary Operator

A more concise way to write the same logic in a single line of code.

String result = (num % 2 == 0) ? "Even" : "Odd";
System.out.println(num + " is " + result);

Method 3: Bitwise AND Operator

For advanced performance optimization, you can check the last bit of the number. If (num & 1) == 0, the number is even.

Interview Insight:

Bitwise operations are faster than arithmetic operations because they work directly on binary data. It's a great way to impress interviewers!

Master Java with 4Achievers

Go from basic syntax to advanced enterprise frameworks like Spring Boot. Join the top-rated Java bootcamp in India with 100% placement assistance.

Key Steps

  • 1. Import the java.util.Scanner class.
  • 2. Take user input using nextInt().
  • 3. Apply the condition num % 2 == 0.
  • 4. Print the result using System.out.println().

Placement Prep

Looking for more Java practice problems? Check our comprehensive question bank.

© 2026 4Achievers Blogs. Built for aspiring Java developers.