How to check Odd and Even Number in java

import java.util.Scanner;
class OddOrEven {
  public static void main(String args[]) {
    int x;
    System.out.println("Enter an integer to check if it is odd or even ");
    Scanner in = new Scanner(System.in);
    x = in.nextInt();
    if (x % 2 == 0)
      System.out.println("You entered an even number.");
    else
      System.out.println("You entered an odd number.");

  }
}

Step-by-Step Explanation:

1. Import Scanner class:

 import java.util.Scanner;

 It is used to import the Scanner class from java.util package. This class reads input from users.

2. Class Declaration:

class OddOrEven

Declare a class named OddOrEven, that holds the logic to decide if an integer passed as a parameter is odd or even.

3. Declaration of Main Method:

 public static void main(String args[]) {

  // This will be where program execution will begin

4. Declaration of the Integer Variable

int x;

A variable x of type int is declared to hold the number taken as input by the user.

5. Getting User Input

System.out.println(“Enter an integer to check if it is odd or even “);

This statement prints out a message requesting the user to enter an integer so that the program can determine if it is odd or even.

6. Create the Scanner Object:

 Scanner in = new Scanner(System.in);

 A Scanner object called in is created to read input from the console.

7. Read the Integer Input:

 x = in.nextInt();

 The program reads an integer input given by the user and assigns it to the variable x.

8. Check If the Number Is Even or Odd:

if (x % 2 == 0) {

      System.out.println(“You entered an even number.”);

  } else {

System.out.println(“You entered an odd number.”);

  }

The program makes use of the modulus operator (%) to check if the number is divisible by 2 (even).

  • When x % 2 == 0, the number is a multiple of 2 and so is even. The program will print “You entered an even number.”
  • When x % 2!= 0, the number is odd. The program prints “You entered an odd number.”

9. Program Termination

Once the program has printed the output, it ends.

Example Run

Example 1: User Inputs an Even Number

Input

Enter an integer to determine whether it is odd or even

24

  • The user inputs 24, which is a number that is even.
  • The program checks 24 % 2 == 0, which is true, so it prints:

Output:

You input an even number.

Example 2: User Enters an Odd Number

Input:

Enter an integer to see if it’s odd or even

19

User enters 19, which is an integer that’s odd.

Program tests 19 % 2 == 0, which is false prints:

Output:

You typed in an odd number.

Summary of Output

1. For input 24 is an even integer:

  You entered an even number.

2. With the input 19 being an odd integer:

You typed an odd number.

Conclusion

  • The program asks the user for an integer input.
  • It checks the number if it is even that is when the number is divisible by 2 without a remainder, and odd if it is not divisible by 2.
  • The number is in the message of the program whether it is odd or even as a result of checking.

Leave a Comment

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