Java Program to Check Whether a Number is Odd or Even Without Using the Modulus Operator

In this Java program, we will learn how to check whether a number is odd or even without using the modulus (%) operator.

The program uses the bitwise AND (&) operator. In binary representation, an even number always has 0 as its least significant bit, while an odd number has 1.

1. Normal Java Program

import java.util.Scanner;

public class OddEvenWithoutOperator {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = sc.nextInt();
        if ((number & 1) == 0) {
            System.out.println(number + " is Even");
        } else {
            System.out.println(number + " is Odd");
        }
        sc.close();
    }
}

Output

Enter a number: 10
10 is Even

Explanation

The expression:

number & 1

checks the least significant bit of the number.

For example:

10 = 1010
 1 = 0001
-----------
&   0000

Since the result is 0, 10 is even.

For an odd number:

11 = 1011
 1 = 0001
-----------
&   0001

The result is 1, so 11 is odd.


2. Java 8 Program

In Java 8, we can use a lambda expression with the Function functional interface to perform the same operation.

import java.util.Scanner;
import java.util.function.Function;

public class OddEvenJava8 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = sc.nextInt();
        Function<Integer, String> checkOddEven =
                n -> (n & 1) == 0 ? "Even" : "Odd";
        System.out.println(number + " is " + checkOddEven.apply(number));
        sc.close();
    }
}

Output

Enter a number: 15
15 is Odd

Explanation of Java 8 Program

The Java 8 version uses the functional interface:

Function<Integer, String>

The lambda expression performs the odd/even check:

n -> (n & 1) == 0 ? "Even" : "Odd"

The ternary operator returns "Even" when the result of (n & 1) is 0; otherwise, it returns "Odd".

The function is executed using:

checkOddEven.apply(number)

Difference Between Normal Java and Java 8

FeatureNormal JavaJava 8
Approachif-elseLambda expression
Modulus %Not usedNot used
Bitwise operator&&
Java 8 featureNoFunction + Lambda
Beginner friendlyYesMore advanced

Important Note

The phrase “without operator” can be interpreted in different ways.

The programs above do not use the modulus (%) operator, which is the usual meaning of this Java programming question. They do use the bitwise AND (&) operator.

If the requirement is to check odd/even without using any arithmetic or bitwise operator at all, a different approach is required.

Conclusion

Using (number & 1) is an efficient way to determine whether an integer is odd or even without using the modulus operator. The normal Java version is easier for beginners, while the Java 8 version demonstrates the use of lambda expressions and functional interfaces

Backend developer working with Java, Spring Boot, Microservices, NoSQL, and AWS. I love sharing knowledge, practical tips, and clean code practices to help others build scalable applications.

Leave a Reply

Your email address will not be published. Required fields are marked *