Hi Friends,

Today, we are going to find Even or Odd Without using if and switch case.  The question asked here is – Write a program to find out if the given number is odd or even without using conditional statement like if or switch case.

Solution :

It is mentioned that We should not use if and switch case.  That is the clue we should remember here.  Hence, for finding out odd or even without those, we can go for array concept.  Let me try a simple Java program with a String array which consists of just two elements.

Declare the two string values in the array with index o and 1.

o => Even

1=> Odd

Any number divide with two its give remainder as o or 1.  Thus, this remainder can be used in array index value.

Program:

public class EvenOdd {

public static void main(String[] args) {
String evenOdd[] = {“Even”,”Odd”};  // String array with only two elements
int number = 3;
int remainder= number % 2;  // remainder is calculated.
System.out.println(evenOdd[remainder]);
}

}

Output:

Odd