Even Odd Program in Java
Even Odd Program in Java
The number that are completely divisible by 2 are even and the number that leaves remainder are odd numbers. For example, the numbers 2, 0, 4, 8, -2, -16 are even numbers and the numbers -1, 1, 3, 5, 7, 9, 11 are odd. In this section, we have created Java programs with different approaches to check the number is even or odd.
The Modulus Operator Approach
Filename: ForLoopExample.java
public class ForLoopExample { // Method for finding whether the input number is the even or odd static boolean isEven(int number) { if(number % 2 == 0) return true; return false; } public static void main(String argvs[]) { // an array of numbers int arr[] = {23, 0, -111, 444, -66, -100, -101, -79, 122, 198, 3003}; // Iterating over every element of the input array for(int i = 0; i < arr.length; i++) { // Invoking isEven() method and displaying results accordingly if(isEven(arr[i])) { System.out.println(arr[i] + ", is an even number."); } else { System.out.println(arr[i] + ", is an odd number."); } } } }
Output:
23 is an odd number. 0 is an even number. -111 is an odd number. 444 is an even number. -66 is an even number. -100 is an even number. -101 is an odd number. -79 is an odd number. 122 is an even number. 198 is an even number. 3003 is an odd number.
Explanation: We are iterating over every element of the input array and invoking the method isEven(). The method uses the modulus operator (%) that finds the remainder when we divide the number by 2. If we get the remainder 0 the given number is even, else the number is odd.
Iterative Approach
Filename: EvenOddExample1.java
public class EvenOddExample1 { public static void main(String argvs[]) { // Input array int arr[] = {23, 0, -111, 444, -66, -100, -101, -79, 122, 198, 3003}; // Iteratiing over every element of the input array for(int i = 0; i < arr.length; i++) { int temp = arr[i]; // creating a copy if(temp >= 0) // For the non-negative numbers { while(temp > 0) { temp = temp - 2; } // Displaying results if(temp == 0) System.out.println(arr[i] + ", the even number."); else System.out.println(arr[i] + ", the odd number."); } else // For the negative numbers { while(temp < 0) { temp = temp + 2; } // Displaying results if(temp == 0) System.out.println(arr[i] + ", is an even number."); else System.out.println(arr[i] + ", is an odd number."); } } } }
Output:
23 is an odd number. 0 is an even number. -111 is an odd number. 444 is an even number. -66 is an even number. -100 is an even number. -101 is an odd number. -79 is an odd number. 122 is an even number. 198 is an even number. 3003 is an odd number.
Explanation: We know that 0 is a multiple of 2. Therefore, 0 is an even number. Also, the sum of two even numbers is always an even number and the sum of one odd, and one even number is an odd number (For example, 2 + 6 = 8, 7 + 2 = 9). These concepts serve as the basis for distinguishing between even and odd numbers. We try to reduce every element of the input array to 0 by incrementing or decrementing by 2. If we become successful, the number is an even number, otherwise an odd number.
Recursive Approach
Filename: EvenOddExample2.java
public class EvenOddExample2 { // Method for handling non-negative numbers public static void recForNonNeg(int noCpy, int no) { if(noCpy == 0) { System.out.println(no + " is the even number."); return; } else if (noCpy < 0) { System.out.println(no + " is the odd number."); return; } // Recursively reducing the number to 0. recForNonNeg(noCpy - 2, no); } // Method for handling negative numbers public static void recForNeg(int noCpy, int no) { if(noCpy == 0) { System.out.println(no + " is an even number."); return; } else if (noCpy > 0) { System.out.println(no + " is an odd number."); return; } // Recursively reducing the number to 0. recForNeg(noCpy + 2, no); } public static void main(String argvs[]) { // Input array int arr[] = {23, 0, -111, 444, -66, -100, -101, -79, 122, 198, 3003}; // Iteratiing over every element of the input array for(int i = 0; i < arr.length; i++) { if(arr[i] >= 0) { // Invoking the method to handle the non-negative numbers recForNonNeg(arr[i], arr[i]); } else { // Invoking the method to handle the negative numbers recForNeg(arr[i], arr[i]); } } } }
Output:
23 is an odd number. 0 is an even number. -111 is an odd number. 444 is an even number. -66 is an even number. -100 is an even number. -101 is an odd number. -79 is an odd number. 122 is an even number. 198 is an even number. 3003 is an odd number.
Explanation: The concept used in the recursive approach is similar to what we have in the iterative approach. The only difference is instead of using a loop to reduce the input numbers to 0; we are reducing the number using the recursion.
Bitwise AND operator Approach
Filename: EvenOddExample3.java
public class EvenOddExample3 { public static void main(String argvs[]) { // Input array int arr[] = {23, 0, -111, 444, -66, -100, -101, -79, 122, 198, 3003}; // Iterating over every element of the input array for(int i = 0; i < arr.length; i++) { // Checking the left most bit of every number and displaying results accordingly if((arr[i] & 1) == 0) { System.out.println(arr[i] + " is an even number."); } else { System.out.println(arr[i] + " is an odd number."); } } } }
Output:
23 is an odd number. 0 is an even number. -111 is an odd number. 444 is an even number. -66 is an even number. -100 is an even number. -101 is an odd number. -79 is an odd number. 122 is an even number. 198 is an even number. 3003 is an odd number.
Explanation: Apart from getting completely divisible by 2, the even numbers have one more unique feature. To learn about it, observe the following table:
Number | Equivalent binary representation |
1 | 0001 |
2 | 0010 |
3 | 0011 |
4 | 0100 |
5 | 0101 |
6 | 0110 |
7 | 0111 |
8 | 1000 |
9 | 1001 |
10 | 1010 |
11 | 1011 |
12 | 1100 |
Notice the rightmost bit of every number. We learn by the observation that for every odd number, the rightmost bit (or LSB) is 1, and for even numbers, it is 0. With the help of this concept, we are able to distinguish between an even and an odd number.
Comparison of the above-written Approaches
Usually, we should use the bitwise AND (&) or the modulus operator (%) approach to check the number is even or odd. Because these two approaches do not use the loop or recursion. However, during interview time, an interviewer may trick and can ask to write the logic to check the number is even or odd without using the modulus operator and loop. Therefore, we must know the iterative as well as the recursive approach, but, when given the liberty, the first and fourth approach should always take precedence over the second and third one.