Table of contents
1.
Introduction
2.
Using Brute Force - Naive Approach
2.1.
Java
3.
Using Bitwise Operators
3.1.
Java
4.
Using Bitwise OR
4.1.
Java
5.
Using Bitwise AND
5.1.
Java
6.
Using Bitwise XOR
6.1.
Java
7.
By Checking the Least Significant Bit
7.1.
Java
8.
Frequently Asked Questions
8.1.
Why do we use the modulo operation to check if a number is odd or even?
8.2.
Are bitwise operations faster than using the modulo operation for checking number parity?
8.3.
Can these methods be used with any programming language?
9.
Conclusion
Last Updated: Jun 3, 2024
Easy

Odd or Even Program in Java

Author Riya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java programming, one of the basic tasks we need to learn is how to check if a number is odd or even. This task is important because it helps you make decisions in your programs based on number types. An odd number is any integer that cannot be divided evenly by 2, meaning it has a remainder of 1 when divided by 2. An even number is any integer that can be divided evenly by 2, with no remainder. 

Odd or Even Program in Java

In this article, we'll see different methods to determine the nature of a number using Java. We will start with simple checks using the modulo operator and then move on to to more advanced methods that use bitwise operations. 

Using Brute Force - Naive Approach

When you start programming, the simplest way to determine if a number is odd or even is to use the modulo operator (%). This operator tells you what remains after you divide one number by another. Here's how it works for checking numbers:

  • If a number when divided by 2 leaves a remainder of 1, it is odd.
     
  • If a number when divided by 2 leaves no remainder, it is even.


Let's look at a simple Java program that uses this method to check if a number is odd or even:

  • Java

Java

public class OddEvenChecker {

   public static void main(String[] args) {

       // Example number

       int number = 13; // You can change this to test other numbers

       // Check if the number is odd or even using the modulo operator

       if (number % 2 == 0) {

           System.out.println(number + " is even.");

       } else {

           System.out.println(number + " is odd.");

       }

   }

}
You can also try this code with Online Java Compiler
Run Code

Output

13 is odd

In this code:

  1. We declare a class named OddEvenChecker.
     
  2. Inside the main method, we declare an integer variable number and assign it a value.
     
  3. We use an if statement to check if the remainder of the division of number by 2 is 0.
     
  4. If true, it prints that the number is even; otherwise, it prints that the number is odd.
     

Note : This method is straightforward & does not require any complex operations, which makes it perfect choice for every beginner coder.

Using Bitwise Operators

Bitwise operators work directly with the bits of a number, making them a powerful tool in programming, especially when you need to perform checks or manipulations at the binary level. For checking if a number is odd or even, we can use the bitwise AND operator (&).

Here’s the reasoning:

In binary, even numbers always end in 0 (like 10 in binary is 1010).

Odd numbers end in 1 (like 3 in binary is 11).

Using the bitwise AND operator with 1 (number & 1) lets us directly check the last bit of a number:

  • If number & 1 equals 0, the number is even.
     
  • If number & 1 equals 1, the number is odd.
     

Let’s see how we can implement this in a Java program:

  • Java

Java

public class BitwiseOddEvenChecker {

   public static void main(String[] args) {

       // Example number

       int number = 14; // You can change this to test other numbers




       // Using bitwise AND operator to check if the number is odd or even

       if ((number & 1) == 0) {

           System.out.println(number + " is even.");

       } else {

           System.out.println(number + " is odd.");

       }

   }

}
You can also try this code with Online Java Compiler
Run Code

Output

14 is even


In this example:

  1. We define a class named BitwiseOddEvenChecker.
     
  2. We declare an integer number and assign it a value in the main method.
     
  3. We apply the bitwise AND operation between number and 1.
     
  4. Based on the result, we print whether the number is even or odd.
     

Note : This method is efficient and very useful where performance matters, as bitwise operations are generally faster than arithmetic operations.

Using Bitwise OR

The bitwise OR operator (|) can also be utilized in programming to check whether a number is odd or even, although it's less direct than the bitwise AND method. Typically, the bitwise OR isn't used alone to determine the parity of a number, but it can help illustrate how bits operate in conditions related to evenness or oddness.

Here's a general explanation:

  • Bitwise OR compares each bit of two numbers. If at least one of the bits is 1, the result is 1; otherwise, it’s 0.
     
  • For our purposes, we don’t typically use | to check parity directly, but we can modify our approach to see its effect in combination with other operations.
     

Let's see a java program that uses this method : 

  • Java

Java

public class ConceptualORChecker {

   public static void main(String[] args) {

       // Example number

       int number = 15; // You can change this to test other numbers

       // Conceptually using bitwise OR to check if the number is odd or even

       int result = number | 1;     

       // The result will always be odd if the original number is odd

       if (result == number) {

           System.out.println(number + " is odd.");

       } else {

           System.out.println(number + " is even."); // This line is for demonstration and typically won't execute in this setup.

       }

   }

}
You can also try this code with Online Java Compiler
Run Code

Output

15 is odd.


In this code:

  1. We define a class called ConceptualORChecker.
     
  2. Inside the main method, we declare an integer number and assign a value.
     
  3. We perform a bitwise OR operation between number and 1.
     
  4. We compare the result with the original number. If they are the same, it implies the last bit was already 1 (odd number).

Using Bitwise AND

The bitwise AND operator (&) is another useful tool in checking whether a number is odd or even, and it is one of the most straightforward methods when working directly with binary values of numbers. This method focuses on the least significant bit (LSB) of the number, which determines its oddness or evenness.

Here’s how it works:

  1. In binary representation, the least significant bit (LSB) is the rightmost bit. For even numbers, this bit is 0, and for odd numbers, it is 1.
     
  2. When you perform a bitwise AND between a number and 1 (number & 1), it checks just the LSB.
     
  3. If the LSB is 0 (as in even numbers), the result of the operation will be 0.
     
  4. If the LSB is 1 (as in odd numbers), the result will be 1.
     

Let’s see a Java example of this method:

  • Java

Java

public class BitwiseANDEvenOddChecker {

   public static void main(String[] args) {

       // Example number

       int number = 22; // You can change this to test other numbers

       // Using bitwise AND to determine if the number is even or odd

       if ((number & 1) == 0) {

           System.out.println(number + " is even.");

       } else {

           System.out.println(number + " is odd.");

       }

   }

}
You can also try this code with Online Java Compiler
Run Code

Output

22 is even


In this code:

  1. The class BitwiseANDEvenOddChecker is defined.
     
  2. Within the main method, an integer number is declared and assigned a value.
     
  3. The bitwise AND operation (&) is applied between number and 1.
     
  4. Depending on whether the result is 0 or 1, it prints out if the number is even or odd.
     

Note : This method is highly effective and efficient, especially when you need to perform this check multiple times, as it reduces the computational load by avoiding division and directly checks the bit value.

Using Bitwise XOR

The bitwise XOR operator (^) is a tool that can be utilized to toggle certain bits in a number. However, it's not typically used for checking if a number is odd or even, but it could be useful to learn to understand basic working of XOR.

Here's how the XOR operation works:

  • The XOR operation takes two bits and returns 1 if the bits are different, and 0 if they are the same.
     
  • Applying XOR between a number and itself always results in zero because all bits cancel each other out.
     

While XOR isn’t directly used to determine the oddness or evenness of a number, let's see a Java example using this method : 

  • Java

Java

public class BitwiseXORChecker {

   public static void main(String[] args) {

       // Example number

       int number = 5; // You can change this to test other numbers

       // Hypothetical use of XOR to check if the number is odd or even

       int result = number ^ 1; // Toggles the LSB of the number

       // Compare the result with the original number

       if (result == number + 1) {

           System.out.println(number + " is odd.");

       } else {

           System.out.println(number + " is even.");

       }

   }

}
You can also try this code with Online Java Compiler
Run Code

Output

5 is odd


In this code:

  1. We define a class BitwiseXORChecker.
     
  2. Within the main method, we assign a value to the integer number.
     
  3. We perform a bitwise XOR operation between number and 1, which toggles the least significant bit.
     
  4. If toggling the LSB turns the number into the next higher number, it indicates it was odd (since only odd numbers become even when their LSB is toggled from 1 to 0).
     
  5. Conversely, if it doesn't match the pattern, the number is even.

By Checking the Least Significant Bit

Checking the Least Significant Bit (LSB) is perhaps the most direct and efficient way to determine if a number is odd or even. The LSB is the rightmost bit in a number's binary representation, and it directly indicates the number's parity:

  • If the LSB is 0, the number is even because it can be divided completely by 2.
     
  • If the LSB is 1, the number is odd because dividing by 2 leaves a remainder of 1.
     

Let’s see a Java example using this method : 

  • Java

Java

public class LSBChecker {

   public static void main(String[] args) {

       // Example number

       int number = 29; // You can change this to test other numbers

       // Checking the least significant bit to determine parity

       if ((number & 1) == 0) {

           System.out.println(number + " is even.");

       } else {

           System.out.println(number + " is odd.");

       }

   }

}
You can also try this code with Online Java Compiler
Run Code

Output

29 is odd


In this code:

  1. We define a class called LSBChecker.
     
  2. In the main method, an integer number is assigned a value.
     
  3. The expression (number & 1) checks the LSB of the number. This is done by using the bitwise AND operation with 1.
     
  4. If the result is 0, it prints that the number is even; if the result is 1, it prints that the number is odd.
     

Note : This method is not only simple but also very fast, making it ideal for use in programs that require frequent parity checks without the overhead of more complex computations.

Frequently Asked Questions

Why do we use the modulo operation to check if a number is odd or even?

The modulo operation (%) returns the remainder after dividing one number by another. When you check a number with % 2, if the remainder is 0, the number is even; if it's 1, the number is odd. This is a straightforward way to determine the number's parity.

Are bitwise operations faster than using the modulo operation for checking number parity?

Yes, bitwise operations are generally faster than using the modulo operation because they deal directly with the number's bits and require fewer computational steps.

Can these methods be used with any programming language?

While the examples provided are in Java, the concepts of using modulo and bitwise operations apply across most programming languages that support these operations, such as C++, Python, and JavaScript.

Conclusion

In this article, we have discussed various methods to determine if a number is odd or even in Java. We started with the simple modulo method, then we saw more efficient techniques using bitwise operations, such as AND and XOR. These methods offer a better understanding of how numbers are represented and manipulated at the binary level, which provides efficient solution. 

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass