Signup/Sign In

Java Program to Check Number is Even or Odd

In this tutorial, we will learn how to check whether the entered number is even or odd using Java. Even numbers are the numbers that are divisible by 2 and the numbers that are not divisible by 2 are called odd numbers. Here, in this program, we will check whether the number is divisible by 2 or not. If divisible, then it is an even number, and if not then it is an odd number. But before moving further, if you are not familiar with the concept of the conditional statement in java, then do check the article on Conditional Statement.

Input: Enter the number: 6

Output: The entered number is even.

Method 1: Java Program to Check a Number is Even or Odd Number

In this program, we will see how to check whether the number is even or odd when the number is user-defined. This means, that here we will first ask the user to enter the number and then we will check whether the entered number is even or odd.

Algorithm

  1. Start

  2. Create an object of the Scanner class to take input from the user.

  3. Declare a variable to store the number.

  4. Ask the user to initialize the number.

  5. Check whether the number is divisible by 2 or not.

  6. If the number is divisible by 2, then the entered number is even.

  7. If the entered number is not divisible by 2, then the entered number is odd.

  8. Display the output.

  9. Stop.

The below example illustrates the implementation of the above algorithm.

/*Java Program to check whether a number is even or odd*/
import java.util.Scanner;

public class Main
{
  public static void main(String args[])
  {
    //To take input from the user 
    //Create an object of scanner class
    Scanner input = new Scanner(System.in);
    int num;  //Declare a variable
    System.out.println("Enter a number:");
    num = input.nextInt();

    //If number is divisible by 2 then it's an even number
    //else odd number
    if ( num % 2 == 0 )
        System.out.println("The entered number is even");
     else
        System.out.println("The entered number is odd");
  }
}


Enter a number: 6
The entered number is odd

Method 2: Java Program to Check a Number is Even or Odd Number

In this program, we will see how to check whether the number is even or odd using the ternary operator. This means, first we will ask the user to enter the number and then check whether the entered number is even or odd using the ternary operator.

Algorithm:

  1. Start

  2. Create an object of the Scanner class to take input from the user.

  3. Declare a variable to store the number.

  4. Ask the user to initialize the number.

  5. Use a ternary operator to check whether the entered number is even or odd.

  6. If the entered number is divisible by 2, then it is an even number else it is an odd number.

  7. Display the result.

  8. Stop

The below example illustrates the implementation of the above algorithm.

/*Java Program to check whether a number is even or odd*/
import java.util.Scanner;

public class Main
{
  public static void main(String args[])
  {
    //To take input from the user 
    //Create an object of scanner class
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a number: ");
    int num = sc.nextInt();
    //Use Ternary Operator to check
    String check = (num % 2 == 0) ? "even" : "odd";

    System.out.println("The entered number "+ num + " is: " + check);
    
  }
}


Enter a number: 5
The entered number 5 is: odd

Method 3: Java Program to Check a Number is Even or Odd Number

In this program, we will see how to check whether the number is even or odd by using the bitwise XOR. The logic behind using this approach is that the bitwise XOR operation of the even number by 1 increments the value of the number by 1 otherwise it decrements the value of the number by 1 if the value is odd.

Algorithm

  1. Start

  2. Create an object of the Scanner class to take input from the user.

  3. Declare a variable to store the number.

  4. Ask the user to initialize the number.

  5. Check whether the number is even or odd by using bitwise XOR.

  6. If the number after bitwise XOR with 1 is equal to the original number + 1, then it is an even number.

  7. If not equal, then it is an odd number.

  8. Display the result.

  9. Stop.

The below example illustrates the implementation of the above algorithm.

/*Java Program to check whether a number is even or odd*/
import java.util.Scanner;

public class Main
{
  public static void main(String args[])
  {
    //To take input from the user 
    //Create an object of scanner class
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a number: ");
    int num = sc.nextInt();
    //Check Using Bitwise XOR
    if ((num ^ 1) == num + 1) 
    { 
         System.out.println("The entered number "+ num +" is Even"); 
    } 
    else 
    { 
        System.out.println("The entered number "+ num +" is Odd"); 
    } 
        
  }
}


Enter a number: 52
The entered number 52 is Even



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.