PRASHNIKAप्रश्निका
All Computer Science notes

Evil Number Program in Java (ISC Practical)

Updated 23 Sep 2026

An Evil Number is a positive whole number that contains an even number of 1s in its binary (base-2) equivalent. Numbers with an odd count of 1s in binary are called Odious Numbers.

Definition

A positive integer $N$ is an Evil Number if the count of set bits (digit 1s) in its binary representation is even.

  • Example 1: $N = 9$
    Convert 9 to binary by successive division by 2:
    • $9 \div 2 = 4$, remainder $1$
    • $4 \div 2 = 2$, remainder $0$
    • $2 \div 2 = 1$, remainder $0$
    • $1 \div 2 = 0$, remainder $1$
    Reading the remainders from bottom to top, the binary form is $1001_2$.
    Count of 1s: $2$ (even).
    Therefore, 9 is an Evil Number.
  • Example 2: $N = 15$
    Convert 15 to binary by successive division by 2:
    • $15 \div 2 = 7$, remainder $1$
    • $7 \div 2 = 3$, remainder $1$
    • $3 \div 2 = 1$, remainder $1$
    • $1 \div 2 = 0$, remainder $1$
    Binary form: $1111_2$.
    Count of 1s: $4$ (even).
    Therefore, 15 is an Evil Number.
  • Example 3: $N = 7$
    Convert 7 to binary by successive division by 2:
    • $7 \div 2 = 3$, remainder $1$
    • $3 \div 2 = 1$, remainder $1$
    • $1 \div 2 = 0$, remainder $1$
    Binary form: $111_2$.
    Count of 1s: $3$ (odd).
    Therefore, 7 is not an Evil Number (it is an Odious Number).

Algorithm

  1. Read a number $N$ from the user.
  2. Validate whether $N > 0$.
    • If not, mark the input as invalid and stop - there is no binary form to check.
  3. If valid, initialise a counter variable count = 0 to store the number of 1s.
  4. Count the 1s directly using repeated division by 2 (there is no need to build the binary number):
    • While $N > 0$:
      • Find the remainder $R = N \pmod 2$.
      • If $R == 1$, increment count by 1.
      • Divide $N$ by 2 ($N = N / 2$).
  5. If the input was invalid, display an error message.
  6. Otherwise, check if count is divisible by 2 (count % 2 == 0) and display that the number is an Evil Number; otherwise display that it is not.

Program

import java.util.Scanner;

public class EvilNumber
{
    int num;        // the number entered by the user
    int count;      // how many 1s there are in the binary form of num
    boolean valid;  // whether num turned out to be a positive number

    // Step 1: read the number from the user
    public void input()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        num = sc.nextInt();
    }

    // Step 2: only positive numbers can be Evil, so check that first and
    // stop right away if not - there is nothing to count for the rest.
    // Steps 3 and 4: count the 1s in the binary form of num. Each time we
    // divide by 2, the remainder (n % 2) is the next binary digit, read
    // from right to left, so we only need to count the remainders that
    // are 1 - we never have to build the binary number.
    public void check()
    {
        if (num <= 0)
        {
            valid = false;
            return;
        }
        valid = true;
        int n = num;    // work on a copy, so num is still there for show()
        count = 0;
        while (n > 0)
        {
            if (n % 2 == 1)    // this binary digit is a 1
                count++;
            n = n / 2;         // drop that digit and move to the next one
        }
    }

    // Steps 5 and 6: an invalid number is neither Evil nor Odious; otherwise
    // an even number of 1s means the number is Evil
    public void show()
    {
        if (!valid)
            System.out.println("INVALID INPUT");
        else if (count % 2 == 0)
            System.out.println(num + " IS AN EVIL NUMBER");
        else
            System.out.println(num + " IS NOT AN EVIL NUMBER");
    }

    public static void main(String args[])
    {
        EvilNumber ob = new EvilNumber();
        ob.input();
        ob.check();
        ob.show();
    }
}

Variable description table

VariableData TypePurpose
numintstores the number entered by the user
countintcounts how many 1s appear in the binary form of num
validbooleanwhether num turned out to be a positive number, decided in check()
nintlocal copy of num, divided by 2 on each pass of the loop inside check()
scScannerreads the number typed by the user
argsString[]command-line arguments of main (unused)
obEvilNumberobject used to call input(), check() and show()

Sample input and output

Enter a number: 9
9 IS AN EVIL NUMBER
Enter a number: 7
7 IS NOT AN EVIL NUMBER
Enter a number: -5
INVALID INPUT

Dry run

Input: num = 9

Stepnn % 2countn after n / 2
Start90
19114
24012
32011
41120

Loop ends (n = 0). count = 2, which is even → 9 IS AN EVIL NUMBER.

For num = -5, check() sets valid = false and returns before the loop ever runs; show() then prints INVALID INPUT without looking at count.

Variations

  1. Range-based generation: "Accept two numbers as a range and print all Evil Numbers that lie between them."
  2. Range-based counting: "Accept a range and count how many Evil Numbers exist in it."
  3. Evil vs Odious classification: "Accept a number and state whether it is Evil or Odious (both classifications, not just one)."
  4. Range-validated input: "Accept a positive integer $N$ within a given range, such as $1 \le N \le 1000$. If $N$ lies outside the range, display INVALID INPUT; otherwise state whether it is an Evil Number."