Daily Java Challenge #2 – Check If a Number Is Even or Odd (Beginner Level)

image
  • Save
Check If a Number Is Even or Odd

🧠 Problem Statement

Write a Java program to check whether a number is even or odd.


πŸ“₯ Input

A number entered by the user, for example:
7


πŸ“€ Output

A message:
7 is an odd number.


πŸ’‘ Simple Explanation

Let’s keep it super simple:

  • If a number can be divided by 2 without any leftover, it’s called an even number (like 2, 4, 6).
  • If there’s a leftover when dividing by 2, it’s called an odd number (like 1, 3, 5).

We use the % symbol (called the modulus operator) in Java to find out what’s left over after dividing.


🧾 Java Code

import java.util.Scanner;

public class EvenOddCheck {
    public static void main(String[] args) {
        // Step 1: Take input from the user
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();

        // Step 2: Check if number is even or odd
        if (number % 2 == 0) {
            System.out.println(number + " is an even number.");
        } else {
            System.out.println(number + " is an odd number.");
        }
    }
}

πŸ” Step-by-Step (Very Lame Language πŸ˜‰)

  1. We ask the user to enter a number.
  2. We use % to check if it divides cleanly by 2.
  3. If number % 2 == 0, then the number is even.
  4. Otherwise, it’s odd.
  5. We print the result.

πŸ“š Example

πŸ‘‰ User enters: 12
βœ… Output: 12 is an even number.

πŸ‘‰ User enters: 9
βœ… Output: 9 is an odd number.


🎯 Tiny Challenge for You

Can you figure out whether a number is even or odd without using %?
(Hint: Use division and multiplication 😎)


πŸ“ Stay tuned for tomorrow’s challenge!
Like, comment, or share if you’re learning Java with us daily! πŸ’›

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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