4

How to find whether a number is odd or even, without using if condition or ternary operators in Java?

This question is given by my teacher. He also give me a hint that it is possible by using a bitwise operator.

Ed Staub
  • 15,480
  • 3
  • 61
  • 91
user3122903
  • 43
  • 1
  • 1
  • 4
  • Are you sure your teacher meant `without using if condition` and not `without using modulo operator`? – Pshemo Dec 20 '13 at 13:58
  • 1
    Yes, he said without using if-else conditon – user3122903 Dec 20 '13 at 13:59
  • 1
    Disclaimer: the ternary conditional operator "?:" IS a if-then-else – Toni Toni Chopper Dec 20 '13 at 14:02
  • @ToniToniChopper Your comment made 3 answers invalid :) – Suresh Atta Dec 20 '13 at 14:06
  • Do you mean "odd or even", or "odd or even parity" ? They are two different things. Your title says "parity", but the question just says "odd or even". – Paul R Dec 20 '13 at 17:56
  • possible duplicate of [How do I check if an integer is even or odd using bitwise operators](http://stackoverflow.com/questions/5700911/how-do-i-check-if-an-integer-is-even-or-odd-using-bitwise-operators) – Paul R Dec 20 '13 at 17:57
  • Be careful with the answers recommending `% 2`. It will give -1 for negative odd numbers. If you use that to index an array, or if you compare it to 1, you're in trouble. `& 1` doesn't have that problem. – harold Dec 22 '13 at 07:34

12 Answers12

18

There are few ways to not use if and get behavior that will be same as if if was used, like ternary operator condition ? valueIfTrue : valueIfFalse or switch/case.

But to be tricky you can also use arrays and try to figure some transformation of our value to proper array index. In this case your code could look like

int number = 13;
String[] trick = { "even", "odd" };
System.out.println(number + " is " + trick[number % 2]);

output:

13 is odd

You can change number % 2 with number & 1 to use suggestion of your teacher. Explanation of how it works can be found here.

Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
10

Consider a number's representation in binary format (E.g., 5 would be 0b101). An odd number has a "1" as its singles digit, an even number had a zero there. So all you have to do is bitwise-and it with 1 to extract only that digit, and examine the result:

public static boolean isEven (int num) {
    return (num & 1) == 0;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
6
int isOdd = (number & 1);      

isOdd will be 1 if number is odd, otherwise it will be 0.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
umanganiello
  • 756
  • 4
  • 7
2

Did you mean something like this?

boolean isEven(int value) {
  return value % 2 == 0;
}

boolean isOdd(int value) {
  return value % 2 == 1;
}
Arne Burmeister
  • 20,046
  • 8
  • 53
  • 94
0

Just saw now 'Without using IF'

boolean isEven(double num) { return (num % 2 == 0) }
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
gabriel
  • 2,351
  • 4
  • 20
  • 23
0

Every odd number have 1 at the end of its binary representation.

Sample :

public static boolean isEven(int num) {
    return (num & 1) == 0;
}
SANN3
  • 9,459
  • 6
  • 61
  • 97
0

Just a quick wrapper over the already defined process...

public String OddEven(int n){
String oe[] = new String[]{"even","odd"};
        return oe[n & 1];
}
boxed__l
  • 1,334
  • 1
  • 10
  • 24
0

I would use:

( (x%2)==0 ? return "x is even" : return "x is odd");

One line code.

iPot
  • 23
  • 5
0

Method 1:

System.out.println(new String[]{"even","odd"}[Math.abs(n%2)]); 

Method 2:

System.out.println(new String[]{"odd","even"}[(n|1)-n]);

Method 1 differs from the accepted answer in the way that it accounts for negative numbers as well, which are also considered for even/odd.

prashant
  • 348
  • 3
  • 13
0

enter image description here

import java.util.Scanner;
public class EvenOddExample
{
    public static void main(String[] args) 
    {
        System.out.println("\nEnter any Number To check Even or Odd");
        Scanner sc=new Scanner(System.in);

        int no=sc.nextInt();
        int no1=no;
        while (no>1) 
        {   
            no=no-2;
        }       
        if(no==0)
        {
            System.out.println(no1 +" is evenNumber");
        }
        else
        {
            System.out.println(no1 +" is odd Number");
        }

    }
}
kiner_shah
  • 3,939
  • 7
  • 23
  • 37
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53
0

you can also use bitwise shift operators (number >> 1)<<1 == number then even else odd

Prashu
  • 1
  • 2
-1
# /* **this program find number is odd or even without using if-else,
## switch-case, neither any java library function...*/
##//find odd and even number without using any condition

  class OddEven {
       public static void main(String[] args) {
       int number = 14;
       String[] trick = { "even", "odd" };
       System.out.println(number + " is " + trick[number % 2]);
     }
 }

  /**************OUTPUT*************
  // 14 is even
  // ...................
  //13 is odd