In Java, there exists a unique type of positive whole number known as an Evil number. It possesses an interesting characteristic in that its binary representation contains an even number of 1’s. Although not as well-known as Prime and Armstrong numbers, Evil numbers are not typically a topic of focus during interviews.
Evil and odious numbers are two types of numbers based on the count of ones in their binary representation.
- An evil number is a number that has an even number of ones in its binary representation. For example, 15 is an evil number because its binary representation, 1111, contains an even number of ones.
- On the other hand, an odious number is a number that has an odd number of ones in its binary representation. For instance, 16 is an odious number because its binary representation, 10000, contains an odd number of ones.
- Similarly, 23 is an evil number since its binary representation, 10111, has an even number of ones.
To determine whether a number is evil or not, the following steps can be followed:
- Take a number as input.
- Convert the number to its binary equivalent and save it in a separate variable.
- Count the total number of ones in the binary representation.
- If the count of ones is an even number, then the number is classified as an evil number.
- Conversely, if the count of ones is an odd number, the number is not classified as an evil number.
Let’s write the code to check whether the number is evil or not.
EvilNumberExample.java
//import required classes and packages import Java.util.*; import java.io.*; import java.util.Scanner; //create EvilNumberExample class to check whether the given number is an Evil number or not public class EvilNumberExample { // create checkNumber() method that returns true when it founds number Evil public static boolean checkNumber(int n) { // find the equivalence binary number using user defined convertToBinary() method long binaryNumber = convertToBinary(n); // find total number of 1's in binary number int count = 0; // iterate each digit of binary number while(binaryNumber != 0) { // if the last digit of binary number is 1, increase the count value if(binaryNumber % 10 == 1) count++; // remove the last digit from the number binaryNumber = binaryNumber / 10; } // check whether the value of count is even or odd if(count % 2 == 0) return true; //return true when the value of count is even //return false if the value of the count is false return false; } //create convertToBinary() method to convert the decimal value into binary private static long convertToBinary(int number) { long binaryNumber = 0; int rem = 0; int j = 1; while(number != 0) { rem = number % 2; binaryNumber += rem * j; number = number / 2; j = j * 10; } return binaryNumber; //return the binary equivalent number of the decimal number } //main() method start public static void main(String[] args) { // declare variable in which the user entered value will be store int num = 0; // create scanner class object Scanner sc = new Scanner(System.in); //display custom message System.out.print("Enter a number : "); //get input from user num = sc.nextInt(); // check whether the number is evil number or not if(checkNumber(num)) System.out.println(num + " is an evil number"); else System.out.println(num + " is not an evil number"); } }
Output
FindAllEvilNumber.java
//import required classes and packages import java.util.*; import java.io.*; import java.util.Scanner; //create a class to get all the Evil number in a given range public class FindAllEvilNumber { //main() method start public static void main(String args[]) { int range; //create scanner class object Scanner sc=new Scanner(System.in); //show custom message System.out.println("Enter the value of range"); //store user entered value into variable range range = sc.nextInt(); for(int i = 1; i <= range; i++) if(checkNumber(i)){ System.out.println(i + " is an Evil number"); } } // create checkNumber() method that returns true when it founds number Evil public static boolean checkNumber(int n) { // find the equivalence binary number using user defined convertToBinary() method long binaryNumber = convertToBinary(n); // find total number of 1's in binary number int count = 0; // iterate each digit of binary number while(binaryNumber != 0) { // if the last digit of binary number is 1, increase the count value if(binaryNumber % 10 == 1) count++; // remove the last digit from the number binaryNumber = binaryNumber / 10; } // check whether the value of count is even or odd if(count % 2 == 0) return true; //return true when the value of count is even //return false if the value of the count is false return false; } //create convertToBinary() method to convert the decimal value into binary private static long convertToBinary(int number) { long binaryNumber = 0; int rem = 0; int j = 1; while(number != 0) { rem = number % 2; binaryNumber += rem * j; number = number / 2; j = j * 10; } return binaryNumber; //return the binary equivalent number of the decimal number } }
Output
For a comprehensive understanding of topics such as Evil Number in Java, be sure to follow tutorials.freshersnow.com to expand your knowledge.