Evil Number Program in Java with Examples

In this tutorial, I will be sharing what is an Evil number, examples of Evil number, algorithm, and java program to check whether a given number is an Evil number or not.

Read Also: Magic Number in Java

What is Evil Number

An Evil number is a positive whole number whose binary representation contains even number of 1's.

Examples

Number to check : 23  
Binary representation : 10111 
Binary representation contains even number of 1's : Yes // 23 is an Evil number
 
Number to check : 8118  
Binary representation : 1111110110110
Binary representation contains even number of 1's : Yes // 8118 is an Evil number

Below are examples of numbers which are NOT Evil numbers

Number to check : 19  
Binary representation : 10011 
Binary representation contains even number of 1's : No // 19 is NOT an Evil number
 
Number to check : 47  
Binary representation : 101111 
Binary representation contains even number of 1's : No // 47 is NOT an Evil number

List of Evil numbers from 1 to 10 are : 3, 5, 6, 9, 10

Read Also: Disarium Number in Java

Java Program for Evil Number

import java.util.Scanner;
public class JavaHungry {
    
    public static void checkEvilNumber(Integer inputNumber)
    {
        /* Converting Integer to Binary equivalent,
        we have used java built in method toBinaryString() 
        to convert decimal value to binary */
        String binaryNumber =  Integer.toBinaryString(inputNumber);
        System.out.println("Binary representation : "+binaryNumber);
        // Count number of 1 in binaryNumber
        int countOne = countNumberOfOne(binaryNumber);
        // Check if countOne is even
        if (countOne % 2 == 0)
            System.out.println(inputNumber+" is an Evil Number");
        else
            System.out.println(inputNumber+" is NOT an Evil Number");
    }
    
    public static int countNumberOfOne(String binaryNumber)
    {
        /* Calculate length (number of digits)
         of the binaryNumber */
        int length = binaryNumber.length();
        int count = 0;
        char ch;
        for(int i=0; i < length; i++)
        {
            ch = binaryNumber.charAt(i);
            if(ch == '1')
                count++;
        }
        System.out.println("Number of Ones in Binary Representation : "+ count);
        return count;
    }
    public static void main(String args[]) 
    {
        System.out.println("Enter any number : ");
        Scanner scan = new Scanner(System.in);
        // Store the input number
        Integer inputNumber = scan.nextInt();
        checkEvilNumber(inputNumber);
    }
}

Output:
Enter any number : 
23
Binary representation : 10111
Number of Ones in Binary Representation : 4
23 is an Evil Number

Algorithm for Evil Number

1. Convert the given inputNumber to binary equivalent using toBinaryString(int) method.

2. Calculate the number of 1 in binary equivalent
 
3. Using for loop
     a.  Taking one digit at a time of binaryNumber, check whether it's equal to '1'. Increment count  variable by 1 if the digit is equal to '1'. 

4. Check if variable countOne is even or not
    a. If even, then the inputNumber is an Evil number.
    b. Otherwise, the inputNumber is not an Evil number. 

That's all for this post. Please mention in comments in case you have any questions related to evil number in java with examples.

You may also like:

Happy number program in Java (answer)
Magic number program in Java (solution)
Disarium number program in Java (answer)
Duck number program in Java (answer)
Neon number program in Java (solution)
Bouncy number program in Java (answer)
Special number program in Java (solution)
Pronic number program in Java (answer)
Perfect number program in Java (solution) 

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry