๐Ÿ” Java How-To Examples
Estimated reading: 2 minutes 31 views

๐Ÿ”ข Java Program to Check Even or Odd Number โ€“ Explained with Examples

๐Ÿงฒ Introduction

Checking if a number is even or odd is one of the first and most useful exercises for beginners in Java. It’s commonly used in loops, game logic, condition checks, and data validation.

By the end of this tutorial, you’ll learn:

  • โœ… How to determine if a number is even or odd in Java
  • โœ… Use Scanner to take input from the user
  • โœ… Apply if...else logic with modulo operator

๐Ÿงฎ Rule: Even or Odd Number Logic

  • A number is even if divisible by 2 (i.e., number % 2 == 0)
  • Otherwise, it is odd

๐Ÿ’ป Java Program โ€“ Hardcoded Value

public class EvenOddCheck {
    public static void main(String[] args) {
        int number = 7;

        if (number % 2 == 0) {
            System.out.println(number + " is even.");
        } else {
            System.out.println(number + " is odd.");
        }
    }
}

โœ… Explanation:

  • number % 2 returns the remainder.
  • If remainder is 0, itโ€™s even; else itโ€™s odd.

๐Ÿ“ฅ Java Program โ€“ Using Scanner Input

import java.util.Scanner;

public class EvenOddInput {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = scanner.nextInt();

        if (num % 2 == 0) {
            System.out.println(num + " is even.");
        } else {
            System.out.println(num + " is odd.");
        }

        scanner.close();
    }
}

โœ… Explanation:

  • Scanner reads user input.
  • % (modulus) checks divisibility by 2.

๐Ÿ” Java Method โ€“ Reusable Function

public class EvenOddUtil {
    public static boolean isEven(int num) {
        return num % 2 == 0;
    }

    public static void main(String[] args) {
        int number = 12;
        if (isEven(number)) {
            System.out.println(number + " is even.");
        } else {
            System.out.println(number + " is odd.");
        }
    }
}

โœ… Use this method in projects or logic-based games for clean code.


โš ๏ธ Edge Case Warning

  • Works with positive, negative, and zero
  • 0 is considered even in Java

๐Ÿ“‹ Example Output

Enter a number: 9
9 is odd.
Enter a number: 0
0 is even.

๐Ÿ“Œ Summary

In this article, youโ€™ve learned:

  • โœ… How to check even or odd numbers in Java
  • โœ… Use input from users with Scanner
  • โœ… Write reusable and optimized methods

This logic is foundational for loops, conditional branching, and algorithm design.


โ“ FAQ โ€“ Even or Odd Number in Java

โ“Can this program handle negative numbers?

โœ… Yes! -4 % 2 == 0, so negative even/odd numbers work just fine.

โ“How can I do this with a ternary operator?

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

โœ… This makes the code cleaner and shorter.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Even or Odd Number

Or Copy Link

CONTENTS
Scroll to Top