View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Even Odd Program in Java

Updated on 24/06/202517,811 Views

How can you determine if a number is even or odd in Java—especially when taking input from users?
This basic logic check forms the foundation of many beginner-level coding challenges. An even odd program in Java helps you understand conditional statements, user input handling, and the modulus operator. The core idea is to divide the number by 2 and check the remainder.

In this tutorial, you’ll learn how to write a Java program to check even or odd numbers using the Scanner class. We’ll explain the logic step-by-step, show how to handle user input, and cover some common variations of the program used in interviews and assignments.

Whether you're learning Java or brushing up your logic, this guide will help you implement even-odd checks cleanly and correctly.

Want to build strong Java programming skills from scratch? Check out upGrad’s Software Engineering Courses to learn Java, logic building, and real-world development.

Methods to Check Even Odd Program in Java

Brute Force Naive Approach

public class Main
 {
      public static void main(String[] args) {
           int n= 51;
     //checking whether the number is even or odd
     if (n% 2 == 0)
              System.out.println(n + " is Even");
     else
              System.out.println(n + " is odd");
      }
 }

This Java program uses “public static void main(String[] args)” main method to determine whether the user input variable ‘n’ is odd or even. We have assigned ‘n’ the value 51. If “(n% 2 == 0)” the remainder is equal to zero, the number is even. Finally, “System.out.println()” is used to print the result to the console. The "51 is odd" message is the printed result in this case.

Using bitwise operators (Explain with examples, screenshots, images)

Even Odd Program in Java Using Bitwise Operators

  • Using Bitwise OR

//Java Program to Check if Given Integer is Odd or Even
// Using Bitwise OR
// Importing required classes
import java.util.*;
import java.util.Scanner;
// Main class
public class even_odd {
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing integer variable
        // to be checked
        //int n = 101;
        Scanner sc=new Scanner(System.in);
       System.out.println("Enter a number");
        int n= sc.nextInt();
        System.out.println("number is: " + n);
        // Condition check
        // if n|1 if greater than n then this number is even
       if ((n | 1) > n) {
            // Print statement
            System.out.println("Number is Even");
        }
        else {
            // Print statement
            System.out.println("Number is Odd");
        }
    }
}

Here, the main class “even_odd” is declared with the public access modifier and prompts the user to enter a number using “System.out.println("Enter a number");”. The input number is stored in an integer variable “n” using “int n= sc.nextInt();”. The program checks if it is even or odd using the bitwise OR operator “|.” If the condition is true, the program prints "Number is Even" and ends after printing the result.

  • Using Bitwise AND

//Java Program to Check if Given Integer is Odd or Even
// Using Bitwise AND
// Importing required classes 
import java.util.*;
import java.util.Scanner;
// Main class
public class even_odd {
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing integer variable
        // to be checked
        //int n = 101;
        Scanner sc=new Scanner(System.in);
       System.out.println("Enter a number");
        int n= sc.nextInt();
        System.out.println("number is: " + n);
        // Condition check
        // if n|1 if greater than n then this number is even
       if ((n & 1) == 1) {
            // Print statement
            System.out.println("Number is Odd");
        }
        else {
            // Print statement
            System.out.println("Number is Even");
        }
    }
}

This even odd program in Java using Scanner class uses the bitwise “AND” operator to check if the least significant bit is set to 1 or 0. Variable “n” stores the user input number, which in this case is 7. If the least significant bit returns 1, it is odd; if it returns 0, it is even. After displaying the “Number is Odd” or “Number is Even” message, the program terminates.

  • Using Bitwise XOR

//Java Program to Check if Given Integer is Odd or Even
// Using Bitwise X-OR
// Importing required classes
import java.util.*;
import java.util.Scanner;
// Main class
public class even_odd {
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing integer variable
        // to be checked
        //int n = 101;
        Scanner sc=new Scanner(System.in);
       System.out.println("Enter a number");
        int n= sc.nextInt();
        System.out.println("number is: " + n);
        // Condition check
        // if n|1 if greater than n then this number is even
        if ((n ^ 1) == n + 1) {
            // Print statement
            System.out.println("Number is Even");
        }
        else {
            // Print statement
            System.out.println("Number is Odd");
        }
    }
}

The program imports the necessary classes and creates a “Scanner” object "sc" to prompt the user to enter a number. The program uses the XOR operator to determine if the number is even or odd. If the operation result is equal to ‘n+1’, the number entered is even, if it isn’t, the number is odd.

Here the number 9 is entered for which the result of the operation is not equal to ‘n+1’. Hence, the number is odd.

Even Odd Program in Java by Checking the Least Significant Bit

Another way to check whether a number is odd or even is by checking the LSB of the number. A binary number’s last digit is its LSB. The LSB of an odd number is always 1, and that of an even number is always 0.

Even Odd Program in Java Using a Ternary Operator

A ternary operator replaces the conventional even odd program in Java using the if-else method to implement the naive logic.

import java.util.Scanner;
public class even_odd {
   public static void main(String[] args) 
   {
      Scanner obj = new Scanner(System.in);
      System.out.println("Please enter a number : ");
      int n = obj.nextInt();
      // java odd or even
      String strOutput = (n % 2 == 0) ? "even" : "odd";   
      System.out.println(n + " is " + strOutput);
      obj.close();
   }
}

In this case, a string variable named "strOutput" is declared and initialized to odd or even based on whether "n" (integer variable) is divisible by 2. The value of "strOutput" is set using a ternary operator. If the remainder acquired upon dividing ‘n’ by 2 is 0, it is even for which "strOutput" is set to even; if not, it is odd and "strOutput" is set to odd. The result is printed on the console using the “println()” method.

Check Whether a Number is Even or Odd Using Bitwise Operator

In this instance, we have shown the use of all Bitwise operators, i.e., OR, AND, and XOR, to check whether the input integer variable is even or odd.

//Java Program to Check if Given Integer is Odd or Even
// Using Bitwise OR,AND,XOR
// Importing required classes
import java.util.*;
import java.util.Scanner;
// Main class
public class even_odd {
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing integer variable
        // to be checked
        //int n = 101;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a number");
        int n= sc.nextInt();
        System.out.println("number is: " + n);
        // Condition check
        // if n|1 if greater than n then this number is even
        if ((n | 1) > n) {
            // Print statement
            System.out.println("Number is Even");
        }
        else {
            // Print statement
            System.out.println("Number is Odd");
        }
        System.out.println("Enter a number");
        int x=sc.nextInt();
        System.out.println("number is: " + x);
        // Condition check
        // if x|1 if greater than x then this number is even
        if ((x & 1) != 0) {
            // Print statement
            System.out.println("Number is Odd");
        }
        else {
            // Print statement
            System.out.println("Number is Even");
        }
        System.out.println("Enter a number");
        int y=sc.nextInt();
        System.out.println("number is: " + y);
        // Condition check
        // if y|1 if greater than y then this number is even
        if ((y ^ 1) == y + 1) {
            // Print statement
            System.out.println("Number is Even");
        }
        else {
            // Print statement
            System.out.println("Number is Odd");
        }
    }
}

The "even_odd" class imports required classes and defines a public static method, "main", which takes an array of Strings as input argument. Inside the "main" method, an object of the Scanner class is created to take user input. The Scanner object reads an integer input from the user and stores it in the integer variable 'n'.

We first used the Bitwise operator OR to check whether the input number is odd or even. If '(n | 1)' is greater than ‘n,’ then ‘n’ is even. If ‘n’ is odd, the message "Number is Odd" is printed on the console. Here we have used 11 as the input number.

Next, we used the Bitwise AND to check if the integer input stored in the variable ‘x’ is even or odd. If (x&1) equals 0, it is even; if not, it is odd. The number used in this instance is 7, for which the result "Number is Odd" is printed to the console.

Last, the XOR operator checks the integer variable ‘y’ for even or odd. If (y ^ 1) equals (y + 1), then ‘y’ is even. Otherwise, ‘y’ is odd.

Conclusion

For programmers learning the even odd program in Java, using various approaches is essential for a sound base in coding. It is quite simple and easy to understand. If you are new to programming, learning the Even Odd program is equivalent to learning the basics of coding with Java. Master the basics of Java to build a strong career down the line. Consider enrolling in a professional course to understand the language better.

FAQs

1. What is an even odd program in Java?

An even odd program in Java determines whether a given number is divisible by 2. If the remainder is 0, it’s even; otherwise, it’s odd. This program is one of the simplest ways to practice conditional logic in Java.

2. How do you check if a number is even or odd in Java?

To check if a number is even or odd in Java, use the modulus operator %. If number % 2 == 0, it's even; otherwise, it's odd. This logic can be applied using an if-else statement inside a simple Java program.

3. How to write an even odd program in Java using Scanner?

Use Scanner to take user input in an even odd program in Java. Read the number from the console using nextInt(), then use conditional logic to check and print whether it’s even or odd.

4. What is the difference between even and odd numbers in Java?

In Java, even numbers are integers divisible by 2 without remainder, while odd numbers leave a remainder of 1. This distinction is used in loops, filters, and logic-driven programming tasks.

5. How does the modulus operator help in even odd logic?

The % operator returns the remainder after division. In an odd even Java program, checking number % 2 lets you determine whether the number is divisible by 2 (even) or not (odd).

6. Can I write an even odd program in Java using methods?

Yes, you can create a reusable even odd method in Java by writing a function that takes an integer and returns whether it's even or odd. This improves code reusability and clarity in larger applications.

7. How to handle user input in Java even odd programs?

Use the Scanner class to take input from users in real time. It’s the standard way to read numbers in an even odd program in Java with user input from the console.

8. Can we check even or odd using ternary operators in Java?

Yes, Java allows you to write concise even odd programs using ternary operators, like:
String result = (num % 2 == 0) ? "Even" : "Odd";. This helps reduce lines of code for simple conditions.

9. What are common errors in even odd programs in Java?

Common errors include not importing java.util.Scanner, forgetting to close the input stream, or misusing the % operator. Always validate the input and test edge cases like negative numbers or zero.

10. Can I use a loop to check multiple numbers for even/odd in Java?

Yes, you can use loops like for, while, or do-while to check several numbers in a row. This variation of the odd even Java program is useful in arrays or batch processing tasks.

11. Is this program useful for Java interviews?

Absolutely. A basic even or odd program in Java is commonly asked in beginner interviews to test understanding of control structures, operators, and user input handling. It often leads into more complex logic questions.

image

Take the Free Quiz on Java

Answer quick questions and assess your Java knowledge

right-top-arrow
image
Pavan Vadapalli

Author|900 articles published

Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology s....

image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Explore Our Free Software Tutorials

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918068792934

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.