Java program to check if a number is evil number or not

Java program to check if a number is evil number or not:

This post will show you how to check if a user given number is evil number or not. A positive number is called an Evil number if its binary equivalent holds even number of 1‘s. A number is called even if it is exactly divisible by 2. For example, 2, 4, 6 etc. are even numbers.

Let’s take an example. 15 is a evil number. Because, its binary value is 1111 and it has even number of 1.

Algorithm to check if a number is evil number or not:

We can use the below algorithm to check if a number is Evil or not.

  • Take the number as input from the user.
  • Find the binary equivalent for the number.
  • Count the number of 1 in the binary value.
    • If the count is even, print that it is a evil number. Else, it is not a evil number.

Now, we have to follow one more algorithm to find the binary value of a number.

  • Divide the number by 2 and store the quotient and remainder.
  • Keep dividing the quotient by 2 until it become 0. Also, keep the remainder stored.
  • Once done, if you join the remainders in reverse, it will be the binary value.

Let’s try to convert 23 to binary:

  • 23/2, quotient is 11 and remainder is 1
  • 11/2, quotient is 5 and remainder is 1
  • 5/2, quotient is 2 and remainder is 1
  • 2/2, quotient is 1 and remainder is 0
  • 1/2, quotient is 0 and remainder is 1

So, the binary value is 10111.

Method 1: Java program to check if a number is Evil number or not:

Let’s write a program to check if a user given number is an Evil number or not:

import java.util.Scanner;

public class Main {
    public static long getBinary(int d) {
        long b = 0;
        int multiplier = 1;
        int remainder;

        while (d != 0) {
            remainder = d % 2;
            b += remainder * multiplier;
            multiplier *= 10;
            d /= 2;
        }
        return b;
    }

    public static boolean isEvil(int n) {
        long binary = getBinary(n);

        int oneCount = 0;

        while (binary != 0) {
            if (binary % 10 == 1) {
                oneCount += 1;
            }

            binary /= 10;
        }

        return oneCount % 2 == 0;
    }

    public static void main(String[] args) {
        int no;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a number");
        no = scanner.nextInt();

        if (isEvil(no)) {
            System.out.println(no + " is an evil number");
        } else {
            System.out.println(no + " is not an evil number");
        }
    }
}

Here,

  • getBinary method takes one integer value and returns its binary value in long
    • b is a long variable to hold the binary value.
    • multiplier is used to add the remainder to the end of the binary value, because we need to keep appending the remainders to the end.
    • remainder is used to hold the remainder.
    • The while loop runs until the value of d is 0. It uses the modulo operator to find the remainder, multiply it with the multiplier and adds it to the current value of b.
    • It also updates the value of the multiplier and changes the value of d to d/2
    • Finally, it returns b.
  • isEvil method takes one integer and returns one boolean value. It returns true if the argument integer is evil, else it returns false.
    • It uses getBinary method to find the binary value of the given number.
    • oneCount is a variable to hold the total number of 1 in the binary.
    • The while loop runs until the value of the binary is 0.
    • Inside the loop, it uses %10 to check the last digit of the binary value. If it is 1, it increments the value of oneCount by 1. Also, it removes the last digit from the binary value by dividing it by 10.
    • Once the while loop ends, it checks the total count of 1 and returns true if it is even. Else, it returns false.
  • The main method uses a Scanner object to read the number from the user. It calls isEvil to check if the number is an evil number or not and prints one message based on that.

If you run this program, it will print result as like below:

Enter a number
10
10 is an evil number

Enter a number
9
9 is an evil number

Enter a number
11
11 is not an evil number

Method 2: Java program to check if a number is Evil number or not using one method:

In the above program, we used two methods. But, we can use only one method to check if a number is evil or not. i.e. instead of finding the binary equivalent and finding the total number of 1 again, we can find the total number or 1 while finding the binary value in only one method.

Let’s write down the program:

import java.util.Scanner;

public class Main {
    public static boolean isEvil(int n) {
        int oneCount = 0;
        int remainder;

        while (n != 0) {
            remainder = n % 2;
            if (remainder == 1) {
                oneCount++;
            }
            n /= 2;
        }

        return oneCount % 2 == 0;
    }

    public static void main(String[] args) {
        int no;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a number");
        no = scanner.nextInt();

        if (isEvil(no)) {
            System.out.println(no + " is an evil number");
        } else {
            System.out.println(no + " is not an evil number");
        }
    }
}

Here,

  • We are not building the final binary string. We are only finding the number of 1 in the binary value.
  • It finds the remainder and if it is 1, it increases the value of oneCount by 1.
  • At the end of the loop, it assigns n/2 to n.

It is almost similar to the above program, but it will be faster because we are doing it in one loop.

If you

Enter a number
17
17 is an evil number

Method 3: Java program to check if a number is Evil number or not using toBinaryString:

toBinaryString method can be used to get the binary representation of an integer value. This method is defined as like below:

public static String toBinaryString(int i)

This is a static method and it returns one String. It is defined in the Integer class and we can call this method directly without creating any object of Integer class.

It returns one string value, i.e. the string representation of the integer parameter. Once we get the binary string, we can iterate through the characters of the string one by one and find out the total number of 1 in that string.

Let’s write it down in code:

import java.util.Scanner;

public class Main {
    public static boolean isEvil(int n) {
        int oneCount = 0;
        String binary = Integer.toBinaryString(n);

        for (char c : binary.toCharArray()) {
            if (c == '1') {
                oneCount++;
            }
        }

        return oneCount % 2 == 0;
    }

    public static void main(String[] args) {
        int no;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a number");
        no = scanner.nextInt();

        if (isEvil(no)) {
            System.out.println(no + " is an evil number");
        } else {
            System.out.println(no + " is not an evil number");
        }
    }
}
  • This is similar to the above program. We are using the isEvil method to check if a number is evil or not.
  • The number is converted to a binary string and we are iterating through the characters of the string one by one.
  • If we find any character is 1, we are incrementing the variable that holds the total number of 1 by one.
  • At the end, it returns one boolean value.

If you run this program, it will print similar output.

Enter a number
17
17 is an evil number

Java program to print all evil numbers from 1 to 100:

We can use any of these three methods to find and print all evil numbers in a range. We have to use a loop and for each number in the loop, we can check if it is evil or not and we can print a message.

public class Main {
    public static boolean isEvil(int n) {
        int oneCount = 0;
        String binary = Integer.toBinaryString(n);

        for (char c : binary.toCharArray()) {
            if (c == '1') {
                oneCount++;
            }
        }

        return oneCount % 2 == 0;
    }

    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            if (isEvil(i)) {
                System.out.print(i + " ");
            }
        }
    }
}

In this program, we are using a for loop that runs from 1 to 100 and for each value of i, it checks if it is evil or not. If it is evil, it prints the value of i.

You can change the loop range to any other values as well. If you run this program, it will print:

3 5 6 9 10 12 15 17 18 20 23 24 27 29 30 33 34 36 39 40 43 45 46 48 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 

Java evil number in range

You might also like: