Java Program to check whether a number is odd or even

May 11, 2022, Learn eTutorial


Here we are explaining how to write a java program to check whether a number is odd or even. So we have to declare the class with a public modifier. Then read a number from the user and check the mod 2 of the number is zero or not. If zero then the number is even, else the number is odd.

How to read a number in java?

To read an integer number from the user we can use the Scanner class. First, create a Scanner class object like Scanner sc = new Scanner( call the function nextInt() to read the integer from the user.Example: number = sc.nextInt().

How to check whether a number is odd or even in java?

After declaring the class OddOrEven then we open the main() function. Then we declare the variable number as a datatype integer. Then we read the value of the number from the user. After that by using a if condition we check whether the number % 2 equals zero, if it is zero then we display the number as even, else we display the number as odd.


STEP 1: Declare the class OddOrEven with public modifier 

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Declare the variable number.

STEP 4: Read the value of the number from the user using a scanner.

STEP 5: Check if the number % 2 equals zero then display the number is even.Else do step 6.

STEP 6: Else display the number is odd.

Java Source Code

                                          import java.util.Scanner;
public class OddOrEven 
    public static void main(String[] args) 
        int number;//Declare the variable
        Scanner sc = new Scanner(;//Create the object of Scanner class.
        System.out.print("Enter the number:");
        number = sc.nextInt();//Read the number from the console
        if(number % 2 == 0)
            System.out.println("The number "+number+" is Even ");
            System.out.println("The number "+number+" is Odd ");


Enter the number:5
The number 5 is Odd 

Enter the number:2
The number 2 is Even