
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to check whether a number is even or odd
We are given an integer number as input, and our task is to write a Java program to check whether that number is even or odd. If a number is divisible by 2, then it is an even number otherwise, it is odd.
To verify a given number is even or odd in Java, we can use 3 different ways one with the modulus operator (%), another with the bitwise AND operator (&), and a third one using a switch statement.
Example Scenarios:
Let's understand the problem statement with some example scenarios:
Input: number = 45 Output: result = odd
When you divide 45 by 2, you will get 1 as a remainder. 2 will not completely divide the number. Hence, it is an odd number.
Input: number = 42; Output: result = even
2 will completely divide the number. Hence, it is an even number.
Using the Modulus Operator
The modulus operator (%) is used to find the remainder of a division operation between two numbers. To check whether the given number is even or odd, perform the modulo operation between 2 and the given number. If it returns 0, then it is even, otherwise, it is odd.
Example
In the following example, we are verifying whether the given number is even or odd using the Java Modulus Operator:
public class EvenOdd { public static void main(String[] args) { int my_input; my_input = 45; System.out.println("The number is defined as " +my_input); if(my_input % 2 == 0) System.out.println("The number " +my_input + " is an even number"); else System.out.println("The number " +my_input + " is an odd number"); } }
Output:
The number is defined as 45 The number 45 is an odd number
Using Bitwise AND Operator
In Java, the binary representation of even numbers ends with 0, and odd numbers end with 1. Use the bitwise AND operator between the given number and 1, and then check if the binary representation ends with 0 or not to determine whether it is even or odd.
Example
Now, let's see a practical implementation to check whether a given number is even or odd using the bitwise AND operator:
public class EvenOdd { public static void main(String[] args) { int my_input; my_input = 42; System.out.println("The number is defined as " +my_input); if ((my_input & 1) == 0) System.out.println("The number " + my_input + " is an even number"); else System.out.println("The number " + my_input + " is an odd number"); } }
Output:
The number is defined as 42 The number 42 is an even number
Using switch Statement
All the numbers that are divisible by 2 end with 0, 2, 4, 6, or 8. Instead of dividing the whole number, we will find the last digit of the given number. If the last digit is any of the 0, 2, 4, 6, or 8, it is an even number. If it is not the case, then an odd number.
There are 5 different conditions here, so we can use the switch statement, which accepts multiple cases and prints the result when the corresponding case matches the condition.
Example
In the following Java program, we implement the above logic to check whether a given number is even or odd using switch cases:
public class EvenOdd { public static void main(String[] args) { int my_input; my_input = 166667; System.out.println("The number is defined as " + my_input); // last digit int lastDigit = my_input % 10; switch (lastDigit) { case 0: case 2: case 4: case 6: case 8: System.out.println("The number " + my_input + " is an even number"); break; default: System.out.println("The number " + my_input + " is an odd number"); } } }
Output:
The number is defined as 166667 The number 166667 is an odd number