Even or Odd Number
Check whether a given integer is even or odd.
BeginnerTopic: Module 1: Basic Java Programs
Java Even or Odd Number Program
This program helps you to learn the fundamental structure and syntax of Java programming.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer: ");
int n = sc.nextInt();
if (n % 2 == 0) {
System.out.println(n + " is Even");
} else {
System.out.println(n + " is Odd");
}
sc.close();
}
}Output
Enter an integer: 7 7 is Odd
Understanding Even or Odd Number
A number is even if it leaves remainder 0 when divided by 2 (n % 2 == 0), otherwise it is odd.
Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.