Evil Number Program in Java

In this post, we will develop a Java program to check whether the given number is an evil number or not. We will also develop a Java program to find all evil numbers in a given range.

An Evil number is a positive whole number that has an even number of 1’s in its binary equivalent. Example: The binary equivalent of 9 is 1001, and 1001 contains even numbers of 1’s so 9 is an evil number.

Procedure to check the given number is an evil number or not
1) Take a number
2) Find its Binary equivalent, and store it in a variable “binary”
3) Count number of 1’s in the binary
4) If the number of 1’s is even then it is an evil number else the given number is not an evil number.

We can convert decimal number to binary number manually or we can take the help of in-built method toBinaryString() method.

The first program we will develop by converting decimal number to binary number manually by using loops, and then the second program will be developed by using toBinaryString() method.

import java.util.Scanner;

public class EvilNumber {

   // method to check evil number
   public static boolean isEvil(int number) {

      // convert number into binary equivalent
      long binary = toBinary(number);

      // count number of 1's in binary number
      int count = 0;
      // iterate each digit of binary number
      while(binary!=0) {

         // check last digit is 1
         if(binary%10 == 1) 
         count++;

         // remove last digit
         binary /= 10;
      }

      // check number of 1's
      if(count%2 == 0)
      return true;

      // else return false
      return false;
   }

   private static long toBinary(int decimal) {
      long binary = 0;
      int remainder = 0;
      int i = 1;
      while(decimal != 0) {
         remainder = decimal % 2;
         binary += remainder * i;
         decimal /= 2;
         i *= 10;
      }
      return binary;
   }

   public static void main(String[] args) {
      // declare variables
      int number = 0;

      // read the input 
      Scanner scan =  new Scanner(System.in);
      System.out.print("Enter an integer number:: ");
      number = scan.nextInt();

      // check the number is evil number or not
      if(isEvil(number))
      System.out.println(number+" is an"
		    	+ " evil number");
      else
      System.out.println(number+" is not an"
	      	  	+ " evil number");

      // close Scanner class object
      scan.close();
   }
}

The output for the different test-cases are:-

Enter an integer number:: 2
2 is not an evil number

Enter an integer number:: 3
3 is an evil number

Enter an integer number:: 9
9 is an evil number

Also see:- Special numberMagic numberArmstrong numberPerfect numberEvil NumberSpy NumberSunny number in Java

Using toBinaryString() method

To convert the decimal number to binary number we can also use toBinaryString() method of the Integer class. The prototype of the toBinaryString() method is:-

public static String toBinaryString(int i)

It is a static method of the Integer class so, no need to create the object of the Integer class. We can directly access it by calling through class name as Integer.toBinaryString().

It returns a string representation of the integer argument as an unsigned integer in base 2. The unsigned integer value is the argument plus 2^32 if the argument is negative; otherwise, it is equal to the argument. This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0s. The value of the argument can be recovered from the returned string s by calling Integer.parseUnsignedInt(s, 2).

import java.util.Scanner;

public class EvilNumber {

   // method to check evil number using String
   public static boolean isEvil(int number) {

      // convert number into binary equivalent,
      // toBinaryString() method is given to 
      // convert decimal to binary
      String binary = Integer.toBinaryString(number);

      // count number of 1's
      int count = 0;

      // iterate each character of String
      for(int i=0; i<binary.length(); i++) {
         // check each character is 1 or not
         if(binary.charAt(i) == '1') 
         count++;
      }

      // check number of 1's
      if(count%2 == 0)
      return true;

      // else return false
      return false;
   }

   public static void main(String[] args) {

      // declare variables
      int number = 0;

      // read the input 
      Scanner scan =  new Scanner(System.in);
      System.out.print("Enter an integer number:: ");
      number = scan.nextInt();

      // check the number is evil number or not
      if(isEvil(number))
      System.out.println(number+" is an"
	    		+ " evil number");
      else
      System.out.println(number+" is not an"
     	  		+ " evil number");

      // close Scanner class object
      scan.close();
   }
}

Java program to find all Evil number in a given range

We can also find all the evil number which exist in the given range. For this purpose, we need to take the help of a loop. You can convert decimal to binary manually or you can take the help of toBinaryString() method.

import java.util.Scanner;

public class EvilNumberInRange {

   // method to check evil number using String
   public static boolean isEvil(int number) {

      // convert number into binary equivalent,
      // toBinaryString() method is given to 
      // convert decimal to binary
      String binary = Integer.toBinaryString(number);

      // count number of 1's
      int count = 0;
      // iterate each character of String
      for(int i=0; i<binary.length(); i++) {

         // check each character is 1 or not
	 if(binary.charAt(i) == '1') 
	 count++;
      }

      // check number of 1's
      if(count%2 == 0)
      return true;

      // else return false
      return false;
   }

   public static void main(String[] args) {

      // declare variables
      int minRange = 0, maxRange = 0;

      // create Scanner class object
      Scanner scan = new Scanner(System.in);

      // read inputs
      System.out.print("Enter min value of range:: ");
      minRange = scan.nextInt();
      System.out.print("Enter max value of range:: ");
      maxRange = scan.nextInt();

      // find all Evil numbers 
      System.out.println("The evil numbers from "+
		minRange+" to "+ maxRange+" are:: ");

      for(int i=minRange; i<=maxRange; i++) {
         if(isEvil(i))
	 System.out.print(i+" ");
      }

      // close Scanner class object
      scan.close();
   }
}

The output for the different test-cases are:-

Enter min value of range:: 1
Enter max value of range:: 50
The evil numbers from 1 to 50 are::
3 5 6 9 10 12 15 17 18 20 23 24 27 29 30 33 34 36 39 40 43 45 46 48

Enter min value of range:: 50
Enter max value of range:: 100
The evil numbers from 50 to 100 are::
51 53 54 57 58 60 63 65 66 68 71 72 75 77 78 80 83 85 86 89 90 92 95 96 99

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *