Java Program to Check If a Number is Even or Odd
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.
- Using Brute Force - Naive Approach
- Using if-else Statement
- Using Bitwise Operators
- Using Ternary Operator
- Checking the LSB of the Number.
Let's discuss each method one by one.
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:
- Take the input number from the user.
- Use the modulo operator (%) with 2 to determine the remainder.
- If the remainder is 0, the number is even; otherwise, it's odd.
- 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.
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.
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 1 | Operand 2 | Bitwise OR |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
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.
Result: 16 is an even number; hence, bitwise OR by 1, increment the number by 1 and yields 17.
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 1 | Operand 2 | Bitwise AND (&) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
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.
Result: 6 is an even number; hence, bitwise AND by 1 yields 0.
Result: 17 is an odd number; hence, bitwise AND by 1 yields 1.
Program:
Output:
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 1 | Operand 2 | Bitwise XOR(^) |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Key points:
- Bitwise XOR operation of the even number(in binary form) by 1 increments the value of the number by 1.
- 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.
Result: 12 is an even number; hence, bitwise, XOR by 1 increments the number by 1 and yields 13.
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.