Java Program to find all Evil Number from 0 to 100

Write a Java program to find Evil Number :

What is an Evil number :

Evil number is a non negative number that has even number of 1 in its binary representation. E.g. Binary representation of 5 is 101 . It has two 1 in binary form. So it is an evil number.

Non evil numbers are called Odious number.

Java Program :

In this tutorial, I will show you how to find all evil numbers from 0 to 100 in Java. We will use one for-loop to run from 0 to 100 and for each number we will have to check if it is an evil number or not. If it is an Evil number, print it.

Finding out an Evil number is same as converting a decimal number to binary. We will count the number of 1 while converting. If the count is even, return true , else false.

Let’s take a look into the program :

 * Copyright (C) 2017 codevscolor
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

 * Example class
public class ExampleClass {

    //utility method to print a string
    static void print(String value) {

     * Method to check if a number is evil or not
     * @param no : input number
     * @return : true if it is a evil number , false otherwise
    private static boolean checkEvilNo(int no) {
        //'sum' will contain the count of '1' in binary format
        int sum = 0;

        //below method is same as decimal to binary converter
        while (no > 0) {
            if (no % 2 == 1) {
                //if 1 contains in the binary representation, increase the sum
            no = no / 2;

        //if no of '1' in the binary representation is even, then it is an evil number
        if (sum % 2 == 0) {
            //return true if the number is evil
            return true;

        //return false if the number is not evil
        return false;

    public static void main(String[] args) {

        //find all evil numbers from 0 to 100
        for (int i = 0; i < 100; i++) {
            if (checkEvilNo(i)) {
                print(i + ",");



Output :


Using the similar method, you can also check if a single number is evil or not.

Similar tutorials :