Java Program to Check If a Number is Even or Odd

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

There are various methods for identifying even and odd numbers in Java. It hinges on divisibility by 2. Even numbers, such as those ending in 0, 2, 4, 6, or 8, result in 'Even' when applying number % 2 == 0, like 204 % 2. Conversely, odd numbers end in 1, 3, 5, 7, or 9 and demonstrate 'Odd' through number % 2 == 1, illustrated by (-59) % 2. This fundamental classification employs the modulus operator % in Java.

Methods

There are various methods for checking even odd programs in Java.

  1. Using Brute Force - Naive Approach
  2. Using if-else Statement
  3. Using Bitwise Operators
  4. Using Ternary Operator
  5. Checking the LSB of the Number.

Let's discuss each method one by one.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Method 1: Using Brute Force - Naive Approach

The Brute Force approach is the simplest approach for checking even odd Java programs. The brute force approach follows a straightforward method using conditional statements. It checks the remainder after the division of number with 2. If the remainder is 0, it is an even number. Else, it is an odd number.

Output:

Method 2: Using if...else Statement

This approach utilizes the if...else conditional statement for checking even odd programs in Java.

Approach:

  1. Take the input number from the user.
  2. Use the modulo operator (%) with 2 to determine the remainder.
  3. If the remainder is 0, the number is even; otherwise, it's odd.
  4. Display the result using the if...else statement.

Java Code:

Output: The output depends on the input number provided by the user. For instance:

  • If the user enters 4, the output will be: 4 is an even number.
  • If the user enters 7, the output will be: 7 is an odd number.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Method 3: Using Bitwise Operators

Bitwise operators OR, AND, and XOR can be used to check even odd programs in Java. Depending on the result of bitwise operations performed on the binary numbers.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

1. Using Bitwise OR

Bitwise OR (|): The bitwise OR operator denoted by | compares the binary values of operands with each other and yields a value of 1 if either has bit 1. If both of the bits are 0, the result of that bit is 0, or else the result is 1.

Bitwise OR operators table:

Operand 1Operand 2Bitwise OR
000
011
101
111

Key Points:

  • Bitwise OR with Even Numbers: The result increments by 1, indicating an even number.
  • Bitwise OR with Odd Numbers: The result remains the same, indicating an odd number.

Example: Let's check if a number is even or odd using Bitwise OR.

BitwiseOR Even Example

Result: 16 is an even number; hence, bitwise OR by 1, increment the number by 1 and yields 17.

BitwiseOR Odd example

Result: 19 is an odd number; hence, bitwise OR by 1 doesn't increment the value and yields 19 as it is.

Program:

Output:

2. Using Bitwise AND

Bitwise AND (&): The bitwise AND operator denoted by & compares the binary values of operands with each other and yields a value of 1 if both of the operands have bit 1. If both of the bits are 1, the result of that bit is 1; otherwise, the result is 0.

Bitwise AND operators Table

Operand 1Operand 2Bitwise AND (&)
000
010
100
111

Key points

  • Bitwise AND operation of the even number(in binary form) by 1 will yield 0.
  • Bitwise AND operation of the odd number(in binary form) by 1 will yield 1.

Example: Let's check if a number is even or odd using Bitwise AND.

BitwiseAND Even example

Result: 6 is an even number; hence, bitwise AND by 1 yields 0.

BitwiseAND Odd example

Result: 17 is an odd number; hence, bitwise AND by 1 yields 1.

Program:

Output:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

3. Using Bitwise XOR

Bitwise XOR (^): The bitwise XOR operator denoted by ^ compares the binary values of operands with each other and yields a value of 1 if both have different bits. XOR yields a value of 0 if both operands have same bits.

Bitwise XOR operators table

Operand 1Operand 2Bitwise XOR(^)
000
011
101
110

Key points:

  1. Bitwise XOR operation of the even number(in binary form) by 1 increments the value of the number by 1.
  2. Bitwise XOR operation of the odd number(in binary form) by 1 decrement the value of the number by 1.

The XOR operation is the most efficient way to determine whether a number is even or odd. A binary XOR of a number by one increments that number by 1 if it is an even number and decrements that number by 1 if it is an odd number.

Example: Let's check even odd programs in Java using Bitwise XOR.

BitwiseXOR Even example

Result: 12 is an even number; hence, bitwise, XOR by 1 increments the number by 1 and yields 13.

BitwiseXOR Odd example

Result- 3 is an even number hence, bitwise XOR by 1 decrement the number by 1 and yields 2.

Program:

Output:

Method 4: Using Ternary Operator

The ternary operator in Java denoted by ? is used to evaluate a conditional expression. It is the only conditional operator in Java that takes 3 operands.

Syntax:

The ternary operator evaluates the condition; if it is true, expression1 is stored in the variable, and if it is false, expression2 is stored in the variable.

Output:

Method 5: Checking the LSB of the Number

  • The Least Significant Bit(LSB) is the last digit of the binary number.

Binary representation of 3: 11. The last bit in binary form is that number's Least Significant Bit(LSB). In the case of 3 LSB is 1 since 11.

  • The LSB of an even number is always 0 and that of an odd number is always 1.

Output:

Conclusion

  • Various methods for checking even odd programs in Java, including brute force, if-else statements, bitwise operations, ternary operators, and checking the least significant bit (LSB).
  • An even number divides by 2 with a remainder of 0, while an odd number results in a remainder of 1.
  • Bitwise operations like OR, AND, and XOR offer efficient ways to assess a number's parity by manipulating its binary representation.
  • The ternary operator provides a concise alternative to if...else statements for implementing the basic logic of even-odd checks.
  • The LSB method directly examines a number's binary form, where an LSB of 0 indicates evenness, and 1 signifies oddness, presenting a straightforward approach to determining parity.