‹ 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$
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$
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$
Count of 1s: $3$ (odd).
Therefore, 7 is not an Evil Number (it is an Odious Number).
Algorithm
- Read a number $N$ from the user.
- Validate whether $N > 0$.
- If not, mark the input as invalid and stop - there is no binary form to check.
- If valid, initialise a counter variable
count = 0to store the number of 1s. - 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
countby 1. - Divide $N$ by 2 ($N = N / 2$).
- While $N > 0$:
- If the input was invalid, display an error message.
- Otherwise, check if
countis 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
| Variable | Data Type | Purpose |
|---|---|---|
| num | int | stores the number entered by the user |
| count | int | counts how many 1s appear in the binary form of num |
| valid | boolean | whether num turned out to be a positive number, decided in check() |
| n | int | local copy of num, divided by 2 on each pass of the loop inside check() |
| sc | Scanner | reads the number typed by the user |
| args | String[] | command-line arguments of main (unused) |
| ob | EvilNumber | object used to call input(), check() and show() |
Sample input and output
Enter a number: 9
9 IS AN EVIL NUMBEREnter a number: 7
7 IS NOT AN EVIL NUMBEREnter a number: -5
INVALID INPUTDry run
Input: num = 9
| Step | n | n % 2 | count | n after n / 2 |
|---|---|---|---|---|
| Start | 9 | — | 0 | — |
| 1 | 9 | 1 | 1 | 4 |
| 2 | 4 | 0 | 1 | 2 |
| 3 | 2 | 0 | 1 | 1 |
| 4 | 1 | 1 | 2 | 0 |
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
- Range-based generation: "Accept two numbers as a range and print all Evil Numbers that lie between them."
- Range-based counting: "Accept a range and count how many Evil Numbers exist in it."
- Evil vs Odious classification: "Accept a number and state whether it is Evil or Odious (both classifications, not just one)."
- 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."