+ 1

How does odd and even output work in this code?

public static boolean isOddEven(int num){ return (num < 10) ? true : (num < 100) ? (num % 2 == num / 10 % 2) : (num % 2 == num / 10 % 2) && isOddEven(num / 10); }

1st Oct 2019, 3:48 AM
Juan Debenedetti
Juan Debenedetti - avatar
2 ответов
+ 3
you can just use x % 2 == 0 to check if x is even (true) or odd (false)
1st Oct 2019, 4:26 AM
Anton Böhler
Anton Böhler - avatar
+ 2
this code check if all digits in number are same odd or same even, so isOddEven(13579); //true isOddEven(24680); //true code process number by separate digits by: n/10 (decimal part is lost then) and check last digit if is even by: n%2 as same as next digit: num / 10 % 2 it uses recursion for next digit: at te end call itself and if number is only digit it is true by default: (num < 10) ? true
1st Oct 2019, 10:20 AM
zemiak