How To Check Whether a Number is a Evil Number or Not in Java?



What is Evil Number?

In mathematical terms, an Evil number is a number whose binary representation has exactly an even number of 1's present in it. For example, the binary representation of 3 is 0011. So the number 1's is even the number 3 is an evil number.

A binary number is a number expressed in the base-2 numeral system, it is also known as the binary numeral system. It is always represented using two digits: 0 and 1. Each digit (0, 1, 2, 3,...), character (a, A, b, c, D, ...Z), and symbol (@, #, $,...) used in computing is represented in binary.

In this article, we will discuss how to check if a number is evil by using Java programming language.

Input & Output Scenarios

Following are a few input and output scenarios that will help you to understand how to check evil numbers by doing mathematical calculations:

Scenario 1

Suppose the given number is 20:

Input: num = 20
Output: Yes

The binary number of 20 is 10100. Since the number of 1's is even (i.e., 2), the number 20 is considered an Evil number.

Scenario 2

Suppose we have a number 55:

Input: num = 55
Output: No

The binary of 55 is 110111 since the number of 1's is odd (i.e., 5). The number 55 is not considered an Evil number.

Explanation

Below is the solution explanation that will help you write a program for checking evil numbers:

  • First, find the binary representation of the given number.
  • Count the total number of 1's.
  • Check if the count of 1's is even; if so, print "Yes"; if odd, print "No".

Example 1

In the following example, we convert the given number 15 into a binary number, compare each binary digit with 1, and count if the number of 1's is even, printing that number is an evil number:

public class evilNumber{
   public static void main(String[] args){
   
      int inputNumber = 15;
      System.out.println("The given number is: " + inputNumber);
      
      long binaryOfInputNumber = 0;
      int temp = inputNumber;
      int reminder = 0;
      int i = 1;
      
      //binary conversion
      while(temp != 0){
         reminder = temp % 2;
         binaryOfInputNumber += reminder * i;
         temp = temp / 2;
         i = i * 10;
      }
      
      int count_one = 0;
      
      while(binaryOfInputNumber != 0){
      
         // check whether the unit place consisting 1 or not
         if(binaryOfInputNumber % 10 == 1)
         
         //increment the count value
         count_one++;
         
         // after counting remove the unit place in each iteration
         binaryOfInputNumber = binaryOfInputNumber / 10;
      }
      
      //check whether number of one's is even or odd
      if(count_one % 2 == 0){
         System.out.println("Yes! " + inputNumber + " is an evil number");
      }
      else{
         System.out.println("No! " + inputNumber + " is not an evil number");
      }
   }
}

Output

Below is the output of the above program:

The given number is: 15
Yes! 15 is an evil number

Example 2

The following is another example of checking whether the number 56 is an evil number.

We define a method named checkEvilNumber() that accepts a number as a parameter, converts it to its binary number, and counts the number of 1's. If the number of 1's is even, it displays "Yes", otherwise, it displays "No":

public class evilNumber{
   public static void checkEvilNumber(int num){
      long binaryOfNum = 0;
      int temp = num;
      int reminder = 0;
      int i = 1;
      
      //binary conversion
      while(temp != 0){
         reminder = temp % 2;
         binaryOfNum += reminder * i;
         temp = temp / 2;
         i = i * 10;
      }
      
      int count_one = 0;
      
      while(binaryOfNum != 0){
      
         // check whether the unit place consisting 1 or not
         if(binaryOfNum % 10 == 1)
         
         //increment the count value
         count_one++;
         
         // after counting remove the unit place in each iteration
         binaryOfNum = binaryOfNum / 10;
      }
      
      //check whether number of one's is even or odd
      if(count_one % 2 == 0){
         System.out.println("Yes! " + num + " is an evil number");
      }
      else{
         System.out.println("No! " + num + " is not an evil number");
      }
   }
   public static void main(String[] args){
   
      int inputNumber = 56;
      System.out.println("The given number is: " + inputNumber);
      
      //calling the checkEvilNumber() method check evil number
      checkEvilNumber(inputNumber);
   }
}

Output

The above program produces the following output:

The given number is: 56
No! 56 is not an evil number
Updated on: 2025-05-29T18:24:59+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements