Tech career with our top-tier training in Data Science, Software Testing, and Full Stack Development.
phone to 4Achievers +91-93117-65521 +91-801080-5667
Navigation Icons Navigation Icons Navigation Icons Navigation Icons Navigation Icons Navigation Icons Navigation Icons

+91-801080-5667
+91-801080-5667
Need Expert Advise, Enrol Free!!
Share this article

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

How to Check Even or Odd Numbers in Java: A Simple Program for Beginners

Here's a Java program with clear guidance and comments for students on how to check if a number is even or odd. The program includes detailed explanations in the code, so it's easy to understand the logic behind it.

import java.util.Scanner; // Import Scanner class for taking user input

public class EvenOddChecker {

    // Main method is the entry point of any Java program
    public static void main(String[] args) {
        // Create a Scanner object to get user input
        Scanner scanner = new Scanner(System.in);
        
        // Provide guidance to the user
        System.out.println("Welcome to the Even or Odd Number Checker!");
        System.out.println("Please enter a number to check whether it is even or odd:");
        
        // Taking input from the user
        int number = scanner.nextInt(); // .nextInt() reads an integer from the user

        // Check if the number is even or odd using the modulo operator
        if (number % 2 == 0) { 
            // If the remainder when divided by 2 is 0, it is an even number
            System.out.println("The number " + number + " is EVEN.");
        } else {
            // If the remainder when divided by 2 is not 0, it is an odd number
            System.out.println("The number " + number + " is ODD.");
        }
        
        // Close the scanner to avoid resource leaks
        scanner.close();
    }
}

 

Explanation:

  1. Imports:

    • import java.util.Scanner; — This line imports the Scanner class which is used to read user input from the console.
  2. Scanner Object:

    • Scanner scanner = new Scanner(System.in); — Creates a scanner object to allow input from the user.
  3. User Prompt:

    • System.out.println("Please enter a number to check whether it is even or odd:"); — This gives clear instructions to the user on what to do.
  4. Input:

    • int number = scanner.nextInt(); — This reads an integer input from the user and stores it in the variable number.
  5. Checking Even or Odd:

    • if (number % 2 == 0) — This checks whether the number is divisible by 2 with no remainder. If true, the number is even.
    • else — If the condition above is false, it implies the number is odd.
  6. Output:

    • System.out.println("The number " + number + " is EVEN."); — Prints the result based on the check.
    • System.out.println("The number " + number + " is ODD."); — Prints the result if the number is odd.
  7. Closing Scanner:

    • scanner.close(); — Closes the scanner to release the resources.

How to Run the Program:

  1. Copy the code into a Java file (e.g., EvenOddChecker.java).
  2. Compile the program using javac EvenOddChecker.java in the terminal.
  3. Run the program using java EvenOddChecker.
  4. Enter any integer when prompted to check whether it's even or odd.

This program is simple yet informative, with comments that explain each step for students learning Java.

 

Aaradhya, an M.Tech student, is deeply engaged in research, striving to push the boundaries of knowledge and innovation in their field. With a strong foundation in their discipline, Aaradhya conducts experiments, analyzes data, and collaborates with peers to develop new theories and solutions. Their affiliation with "4achievres" underscores their commitment to academic excellence and provides access to resources and mentorship, further enhancing their research experience. Aaradhya's dedication to advancing knowledge and making meaningful contributions exemplifies their passion for learning and their potential to drive positive change in their field and beyond.

Explore the latest job openings

Looking for more job opportunities? Look no further! Our platform offers a diverse array of job listings across various industries, from technology to healthcare, marketing to finance. Whether you're a seasoned professional or just starting your career journey, you'll find exciting opportunities that match your skills and interests. Explore our platform today and take the next step towards your dream job!

See All Jobs

Explore the latest blogs

Looking for insightful and engaging blogs packed with related information? Your search ends here! Dive into our collection of blogs covering a wide range of topics, from technology trends to lifestyle tips, finance advice to health hacks. Whether you're seeking expert advice, industry insights, or just some inspiration, our blog platform has something for everyone. Explore now and enrich your knowledge with our informative content!

See All Bogs

Enrolling in a course at 4Achievers will give you access to a community of 4,000+ other students.

Email

Our friendly team is here to help.
Info@4achievers.com

Phone

We assist You : Monday - Sunday (24*7)
+91-801080-5667
Register for Free Demo
By clicking the button above, you agree to our Terms of Use and Privacy Policy. We do not share this information.

Whatsapp

Call