C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Operators & Expressions

Java Program: Check if number is Even or Odd

import java.util.Scanner;

 

public class EvenOddCheck {

    public static void main(String[] args) {

 

        Scanner sc = new Scanner(System.in);

 

        System.out.print("Enter a number: ");

        int num = sc.nextInt();

 

        // Check even or odd

        if (num % 2 == 0) {

            System.out.println(num + " is an Even number.");

        } else {

            System.out.println(num + " is an Odd number.");

        }

 

        sc.close();

    }

}

Output

 
OUTPUT 1:

INPUT :
Enter a number: 24

OUTPUT :
24 is an Even number.

OUTPUT 2:
 
INPUT :
Enter a number: 17

OUTPUT :
17 is an Odd number. 
 

Explanation

1. Logic Used

The program uses the modulus operator (%).

  • If number % 2 == 0 → Even
  • Else → Odd

Example:

24 % 2 = 0 → Even

17 % 2 = 1 → Odd

2. Program Flow

  1. Accept an integer from the user
  2. Apply modulus operation
  3. Use if-else to decide
  4. Display the result

3. Why % Operator?

It returns the remainder after division.
Even numbers always leave remainder 0 when divided by 2.