SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Evil Number Program in Java and Python

23 September 2015

Evil Number program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Write a program to input a number and check whether it is an Evil Number or not.

Evil Number: An Evil Number is a positive whole number which has an even number of 1s in its binary equivalent.

Example 1 INPUT: 15 BINARY EQUIVALENT: 1111 NO. OF 1's: 4 OUTPUT: EVIL NUMBER Example 2 INPUT: 26 BINARY EQUIVALENT: 11010 NO. OF 1's: 3 OUTPUT: NOT AN EVIL NUMBER

Algorithm:

Step 1: Start.

Step 2: Accept a positive number from the user.

Step 3: Convert the number to its binary equivalent by repeated division by 2.

Step 4: Store each remainder at the beginning of the binary string.

Step 5: Count the number of 1s in the binary string.

Step 6: Display the binary equivalent and the number of 1s.

Step 7: If the count of 1s is even, display that it is an Evil Number; otherwise, display that it is not an Evil Number.

Step 8: Use a working copy of the number during repeated division by 2.

Step 9: Append each remainder to build the binary representation before counting 1s.

Step 10: Stop.

Explanation:

An evil number is a positive number whose binary representation contains an even number of 1s. The program therefore has two stages: binary conversion and counting 1s.

The decimal number is converted to binary by repeated division by 2. Each remainder gives one binary digit, and the digits are collected in reverse order of generation.

After forming the binary number, the program counts how many digits are equal to 1. This count decides whether the binary representation has even or odd parity.

If the count of 1s is divisible by 2, the number is evil. Otherwise, it is not an evil number. This follows the ISC specimen question definition directly.

The binary conversion loop repeatedly stores the remainder obtained by division by 2. Those remainders are the binary digits, and counting the digit 1 in that result gives the parity required by the question.

An Evil number is identified through its binary representation. The program first converts the decimal number into binary, usually by repeated division by 2. During or after conversion, it counts the number of 1 bits. If the count of 1s is even, the number is Evil; otherwise it is not. This means the program combines base conversion with frequency counting. The actual decimal value is less important than the pattern of bits produced in binary form.

Java Program:

Java
/**
* The class EvilNumber accepts a number, converts it to binary
* and checks whether it is an Evil Number.
*
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class EvilNumber
{
    String toBinary(int n)
    {
        String result = "";

        /*
        * Remainders obtained by division by 2 form the binary digits.
        * Each new remainder is added in front to get the correct order.
        */
        while(n > 0)
        {
            int remainder = n % 2;
            result = remainder + result;
            n = n / 2;
        }

        return result;
    }

    int countOne(String s)
    {
        int count = 0;

        /*
        * Count every occurrence of character '1' in the binary string.
        */
        for(int i = 0; i < s.length(); i++)
        {
            if(s.charAt(i) == '1')
            count++;
        }

        return count;
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        EvilNumber ob = new EvilNumber();

        System.out.print("Enter a positive number: ");
        int n = sc.nextInt();

        String binary = ob.toBinary(n);
        int ones = ob.countOne(binary);

        System.out.println("Binary Equivalent = " + binary);
        System.out.println("Number of Ones = " + ones);

        if(ones % 2 == 0)
        System.out.println(n + " is an Evil Number");
        else
        System.out.println(n + " is not an Evil Number");
    }
}

Equivalent Python Program:

Python
# Read the number and keep any required copy for digit or divisor processing.
# Loops and conditions implement the number-property test step by step.
# Display the result according to the flag/counter/calculated value.

n = int(input("Enter a positive number: "))

copy = n
binary = ""

# Convert the decimal number to binary by repeated division by 2.
while copy > 0:
    remainder = copy % 2
    binary = str(remainder) + binary
    copy = copy // 2

ones = 0

# Count the number of 1's in the binary equivalent.
for ch in binary:
    if ch == '1':
        ones = ones + 1

print("Binary Equivalent =", binary)
print("Number of Ones =", ones)

if ones % 2 == 0:
    print(n, "is an Evil Number")
else:
    print(n, "is not an Evil Number")

Output:

Enter a positive number: 26 Binary Equivalent = 11010 Number of Ones = 3 26 is not an Evil Number Enter a positive number: 420 Binary Equivalent = 110100100 Number of Ones = 4 420 is an Evil Number Enter a positive number: 659 Binary Equivalent = 1010010011 Number of Ones = 5 659 is not an Evil Number

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →