Evil Number | Java program to check if a number is Evil number or Odious Number ; returns number of 1 s from the binary number ; counting 1 s ; Check if number is evil or not ; converting n to binary form ; calculating remainder ; storing the remainders in binary form as a number ; Calling the count_one function to count and return number of 1 s in bin ; Driver Code
class GFG { static int count_one ( int n ) { int c_one = 0 ; while ( n != 0 ) { int rem = n % 10 ; if ( rem == 1 ) c_one = c_one + 1 ; n = n / 10 ; } return c_one ; } static int checkEvil ( int n ) { int i = 0 , bin = 0 , n_one = 0 ; while ( n != 0 ) { int r = n % 2 ; bin = bin + r * ( int ) ( Math . pow ( 10 , i ) ) ; n = n / 2 ; } n_one = count_one ( bin ) ; if ( n_one % 2 == 0 ) return 1 ; else return 0 ; } public static void main ( String [ ] args ) { int i , check , num ; num = 32 ; check = checkEvil ( num ) ; if ( check == 1 ) System . out . println ( num + " ▁ is ▁ Evil ▁ Number " ) ; else System . out . println ( num + " ▁ is ▁ Odious ▁ Number " ) ; } }