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

One of the fundamental tasks when learning a programming language is understanding how to determine if a number is even or odd. This basic concept not only helps in grasping control structures but also provides a foundation for more complex problem-solving techniques. This article will guide you through writing a Java program to check if a number is even or odd, covering the theory, coding, and logic in detail.

Understanding Even and Odd Numbers

Before diving into the Java code, it’s essential to understand what makes a number even or odd. A number is considered even if it is divisible by 2 without leaving a remainder. Conversely, a number is odd if it is not divisible by 2, meaning it leaves a remainder of 1.

Mathematically:

  • Even numbers: 2, 4, 6, 8, etc.
  • Odd numbers: 1, 3, 5, 7, etc.

The concept can be expressed using modulo operation. In Java, the modulo operator is represented by %. When a number is divided by 2:

  • If the remainder is 0, the number is even.
  • If the remainder is 1, the number is odd.

Steps to Write the Program

  1. Set Up the Environment: Ensure you have a Java development environment set up. You can use IDEs like IntelliJ IDEA, Eclipse, or a simple text editor with command line.
  2. Create a New Java Class: Open your IDE or text editor and create a new Java class. For this example, we’ll name the class EvenOddChecker.
  3. Write the Main Method: The main method is the entry point of any Java program. Inside this method, we’ll write the logic to check if a number is even or odd.
  4. Get User Input: We will use the Scanner class to get input from the user. This allows us to check different numbers without changing the code.
  5. Check Even or Odd: Implement the logic using the modulo operator to determine if the entered number is even or odd.
  6. Output the Result: Finally, print the result to the console.

Writing the Code

Here’s the step-by-step implementation of the Java program:

import java.util.Scanner;

public class EvenOddChecker {

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

        // Prompt the user to enter a number
        System.out.println("Enter a number: ");
        int number = scanner.nextInt();

        // Close the scanner object
        scanner.close();

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

Detailed Explanation

  1. Importing the Scanner Class:
import java.util.Scanner;

The Scanner class is part of the java.util package and is used for obtaining input of primitive types like int, double, etc., and strings.

Creating the Main Class:

public class EvenOddChecker {

This line declares a public class named EvenOddChecker. In Java, the filename should match the class name.

Main Method Declaration:

public static void main(String[] args) {

The main method is the entry point of the program. public means it is accessible from anywhere, static means it belongs to the class rather than an instance, and void means it does not return any value.

Creating a Scanner Object:

Scanner scanner = new Scanner(System.in);

This line creates a Scanner object named scanner to read input from the standard input stream (keyboard).

Prompting User Input:

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

System.out.println prints a message to the console asking the user to enter a number. scanner.nextInt() reads the next integer input from the user and stores it in the variable number.

Closing the Scanner:

scanner.close();

It is good practice to close the Scanner object to free up system resources.

Checking Even or Odd:

if (number % 2 == 0) {
    System.out.println(number + " is even.");
} else {
    System.out.println(number + " is odd.");
}
  1. This block uses an if-else statement to check if the number is even or odd. The condition number % 2 == 0 checks if the remainder when number is divided by 2 is zero. If true, it prints that the number is even; otherwise, it prints that the number is odd.

Running the Program

To run the program, save your file as EvenOddChecker.java and follow these steps:

  1. Compile the Program: Open a terminal or command prompt and navigate to the directory where your file is saved. Run the following command:
javac EvenOddChecker.java

This command compiles the Java file and generates a EvenOddChecker.class file.

Run the Program: After compiling, run the program with the following command:

java EvenOddChecker
  1. The program will prompt you to enter a number, and it will display whether the number is even or odd based on your input.

What did we learn from this article?

In this article, we explored how to write a Java program to check if a number is even or odd. We covered the theoretical concepts of even and odd numbers, explained the steps to set up and write the program, and provided a detailed explanation of the code. This simple yet fundamental task is a great way to get familiar with Java programming basics, including input handling, control structures, and basic arithmetic operations. By understanding these concepts, you can build a strong foundation for tackling more complex programming challenges in the future.

Leave a Comment