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.
- 15 is an evil number because in its binary equivalent, i.e., 1111, it has an even number of ones.
- 16 is an odious number because in its binary equivalent, i.e., 10000 has not even number of ones
- 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:
- We first take a number.
- We then find the binary equivalent of this number and store it into another variable.
- We find the total number of ones in the binary number.
- 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
-
- import Java.util.*;
- import java.io.*;
- import java.util.Scanner;
-
-
- public class EvilNumberExample {
-
-
- public static boolean checkNumber(int n) {
-
-
- long binaryNumber = convertToBinary(n);
-
-
- int count = 0;
-
-
- while(binaryNumber != 0) {
-
-
- if(binaryNumber % 10 == 1)
- count++;
-
-
- binaryNumber = binaryNumber / 10;
- }
-
-
- if(count % 2 == 0)
- return true;
-
-
- return false;
- }
-
-
- 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;
- }
-
-
- public static void main(String[] args) {
-
-
- int num = 0;
-
-
- Scanner sc = new Scanner(System.in);
-
-
- System.out.print("Enter a number : ");
-
-
- num = sc.nextInt();
-
-
- if(checkNumber(num))
- System.out.println(num + " is an evil number");
- else
- System.out.println(num + " is not an evil number");
-
- }
- }
Output

Let's implement one more program to get all the Evil numbers in a given range
FindAllEvilNumber.java
-
- import java.util.*;
- import java.io.*;
- import java.util.Scanner;
-
-
- public class FindAllEvilNumber {
-
-
- public static void main(String args[])
- {
- int range;
-
-
- Scanner sc=new Scanner(System.in);
-
-
- System.out.println("Enter the value of range");
-
-
- range = sc.nextInt();
-
- for(int i = 1; i <= range; i++)
- if(checkNumber(i)){
- System.out.println(i + " is an Evil number");
- }
- }
-
-
- public static boolean checkNumber(int n) {
-
-
- long binaryNumber = convertToBinary(n);
-
-
- int count = 0;
-
-
- while(binaryNumber != 0) {
-
-
- if(binaryNumber % 10 == 1)
- count++;
-
-
- binaryNumber = binaryNumber / 10;
- }
-
-
- if(count % 2 == 0)
- return true;
-
-
- return false;
- }
-
-
- 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;
- }
- }
Output