Evil Number Java

The Evil number is another special positive whole number in Java that has an even number of 1’s in its binary equivalent. Unlike Prime and Armstrong numbers, Evil number is not so popular and asked by the interviewers.

The numbers which are not evil are called odious numbers. Let’s some examples of evil and odious numbers.

  1. 15 is an evil number because in its binary equivalent, i.e., 1111, it has an even number of ones.
  2. 16 is an odious number because in its binary equivalent, i.e., 10000 has not even number of ones
  3. 23 is also an evil number because it has an even number of ones in its binary equivalent, i.e., 10111.

In order to check whether the number is evil or not, we have to follow the following steps:

  1. We first take a number.
  2. We then find the binary equivalent of this number and store it into another variable.
  3. We find the total number of ones in the binary number.
  4. If we found an even number of ones in the binary equivalent number, then the number is an evil number. Else the given number is not an evil number.

Note: To convert a decimal number into binary, we can use the toBinaryString() in-built method or do it manually by using the loop.

Let’s implement the code to check whether the number is evil or not.

EvilNumberExample.java

  1. //import required classes and packages  
  2. import Java.util.*;   
  3. import java.io.*;   
  4. import java.util.Scanner;  
  5.   
  6. //create EvilNumberExample class to check whether the given number is an Evil number or not  
  7. public class EvilNumberExample {  
  8.   
  9.     // create checkNumber() method that returns true when it founds number Evil   
  10.     public static boolean checkNumber(int n) {  
  11.   
  12.         // find the equivalence binary number using user defined convertToBinary() method  
  13.         long binaryNumber = convertToBinary(n);  
  14.   
  15.         // find total number of 1’s in binary number  
  16.         int count = 0;  
  17.   
  18.         // iterate each digit of binary number  
  19.         while(binaryNumber != 0) {  
  20.   
  21.             // if the last digit of binary number is 1, increase the count value  
  22.             if(binaryNumber % 10 == 1)   
  23.                 count++;  
  24.   
  25.             // remove the last digit from the number  
  26.             binaryNumber = binaryNumber / 10;  
  27.         }  
  28.   
  29.         // check whether the value of count is even or odd  
  30.         if(count % 2 == 0)  
  31.             return true;    //return true when the value of count is even  
  32.   
  33.         //return false if the value of the count is false  
  34.         return false;  
  35.     }  
  36.   
  37.     //create convertToBinary() method to convert the decimal value into binary  
  38.     private static long convertToBinary(int number) {  
  39.         long binaryNumber = 0;  
  40.         int rem = 0;  
  41.         int j = 1;  
  42.         while(number != 0) {  
  43.             rem = number % 2;  
  44.             binaryNumber += rem * j;  
  45.             number = number / 2;  
  46.             j = j * 10;  
  47.         }  
  48.           
  49.         return binaryNumber;    //return the binary equivalent number of the decimal number  
  50.     }  
  51.       
  52.     //main() method start  
  53.     public static void main(String[] args) {  
  54.       
  55.         // declare variable in which the user entered value will be store  
  56.         int num = 0;  
  57.   
  58.         // create scanner class object   
  59.         Scanner sc =  new Scanner(System.in);  
  60.           
  61.         //display custom message  
  62.         System.out.print(“Enter a number : “);  
  63.           
  64.         //get input from user  
  65.         num = sc.nextInt();  
  66.   
  67.         // check whether the number is evil number or not  
  68.         if(checkNumber(num))  
  69.             System.out.println(num + ” is an evil number”);  
  70.         else  
  71.             System.out.println(num + ” is not an evil number”);  
  72.   
  73.     }  
  74. }  

Output

Evil Number Java

Let’s implement one more program to get all the Evil numbers in a given range

FindAllEvilNumber.java

  1. //import required classes and packages  
  2. import java.util.*;   
  3. import java.io.*;   
  4. import java.util.Scanner;  
  5.   
  6. //create a class to get all the Evil number in a given range  
  7. public class FindAllEvilNumber {  
  8.   
  9.     //main() method start  
  10.     public static void main(String args[])  
  11.     {  
  12.         int range;  
  13.           
  14.         //create scanner class object  
  15.         Scanner sc=new Scanner(System.in);  
  16.           
  17.         //show custom message  
  18.         System.out.println(“Enter the value of range”);  
  19.           
  20.         //store user entered value into variable range  
  21.         range = sc.nextInt();  
  22.   
  23.         for(int i = 1; i <= range; i++)  
  24.             if(checkNumber(i)){  
  25.                 System.out.println(i + ” is an Evil number”);  
  26.             }  
  27.     }  
  28.       
  29.     // create checkNumber() method that returns true when it founds number Evil   
  30.     public static boolean checkNumber(int n) {  
  31.   
  32.         // find the equivalence binary number using user defined convertToBinary() method  
  33.         long binaryNumber = convertToBinary(n);  
  34.   
  35.         // find total number of 1’s in binary number  
  36.         int count = 0;  
  37.   
  38.         // iterate each digit of binary number  
  39.         while(binaryNumber != 0) {  
  40.   
  41.             // if the last digit of binary number is 1, increase the count value  
  42.             if(binaryNumber % 10 == 1)   
  43.                 count++;  
  44.   
  45.             // remove the last digit from the number  
  46.             binaryNumber = binaryNumber / 10;  
  47.         }  
  48.   
  49.         // check whether the value of count is even or odd  
  50.         if(count % 2 == 0)  
  51.             return true;    //return true when the value of count is even  
  52.   
  53.         //return false if the value of the count is false  
  54.         return false;  
  55.     }  
  56.   
  57.     //create convertToBinary() method to convert the decimal value into binary  
  58.     private static long convertToBinary(int number) {  
  59.         long binaryNumber = 0;  
  60.         int rem = 0;  
  61.         int j = 1;  
  62.         while(number != 0) {  
  63.             rem = number % 2;  
  64.             binaryNumber += rem * j;  
  65.             number = number / 2;  
  66.             j = j * 10;  
  67.         }  
  68.           
  69.         return binaryNumber;    //return the binary equivalent number of the decimal number  
  70.     }  
  71. }  

Output

Evil Number Java

Leave a Comment