Check the number is even or odd

This is a Java program that determines whether an entered number is even or odd. The program uses the Scanner class to take an input from the user and then closes the reader object once the input has been taken.

Next, it checks the number’s parity using the modulo operator. If the result of x % 2 is 0, then the number is even, and the program outputs “Number X is even”. If the result of x % 2 is not 0, then the number is odd, and the program outputs “Number X is odd”.

import java.util.Scanner;

class EvenOrOdd {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int x;

		System.out.print("Enter a value for x: ");
		x = reader.nextInt();
		reader.close();

		if (x % 2 == 0) {
			System.out.print("Number " + x + " is even");
		} else {
			System.out.print("Number " + x + " is odd");
		}
	}
}
Output
Enter a value for x: 6 
Number 6 is even