static void method1(int number){ if(number % 10 == 1 || number % 10 == 3 || number % 10 == 5 || number % 10 == 7 || number % 10 == 9){ System.out.println("ODD"); } else{ System.out.println("EVEN"); } }
Method 2:-
static void method2(int number){ if(number % 2 == 1){ System.out.println("ODD"); } else{ System.out.println("EVEN"); } }
Method 3:- Using integer class to convert int to binary and check whether least signifiant bit is a one or zero.
lsb = 1 => odd
lsb = 0 => even
static void method3(int number){ if(Integer.lowestOneBit(number) == 1){ System.out.println("ODD"); } else{ System.out.println("EVEN"); } }
No comments:
Post a Comment