0

Give me a Java code to check whether the number is even or odd but without using any if else or conditional statement.

use of "?:" is also not allowed

22nd Dec 2016, 11:27 AM
Dhairya Agarwal
Dhairya Agarwal - avatar
2 Answers
+ 6
public static void main(String[] args){ System.out.println(isEven(6)); } private static boolean isEven(int number){ return number % 2 == 0; }
22nd Dec 2016, 11:35 AM
Uran Kajtazaj
Uran Kajtazaj - avatar
+ 3
Another solution, same return value: private static boolean isEven(int number) { return (number & 1); # logical and ( bits operating, no comparison : mostly efficace if intensive use / no special need of encapsulate in function ) }
22nd Dec 2016, 12:24 PM
visph
visph - avatar