An Evil Number is a non-negative integer whose binary representation contains an even number of 1s; numbers with an odd count of 1s are called Odious Numbers. For example, 9 in binary is 1001 (two 1s, so Evil), while 15 is 1111 (four 1s, Evil) and 7 is 111 (three 1s, Odious). In Java, repeatedly dividing temp by 2 and checking temp % 2 == 1 counts every 1-bit directly without building a binary string or array.
- 1. Binary Parity: Evil vs. Odious Integers
- 2. Repeated Division-by-2 Trace for 9 and 23
- 3. Handling Zero and Negative Input Bounds
- 4. EvilNumber.java (BlueJ)
- 5. Variable Description Table (VDT) — EvilNumber
- 6. Common Mistakes & Top Scorer Tips
- 7. Interactive Concept Check Quiz
- 8. Frequently Asked Questions
1. Binary Parity: Evil vs. Odious Integers
Every non-negative integer partitions cleanly into one of two binary-parity families: Evil Numbers, whose base-2 expansion contains an even tally of 1s, and Odious Numbers, whose expansion contains an odd tally of 1s. For instance, 3 (binary 11), 5 (binary 101), 6 (binary 110), 9 (binary 1001), and 15 (binary 1111) each carry two or four set bits and belong to the Evil family. Conversely, 1 (1), 2 (10), 4 (100), and 7 (111) carry one or three set bits and are Odious. In CISCE Computer Applications and Computer Science papers, Evil Number questions almost always ask the student to print both the binary equivalent and the count of 1s before declaring the verdict, testing decimal-to-binary conversion and parity checking simultaneously in one compact loop.
- Even count of binary 1s (0, 2, 4, 6...) -> Evil Number.
- Odd count of binary 1s (1, 3, 5, 7...) -> Odious Number.
- Examples: 0, 3, 5, 6, 9, 10, 12, 15, 17, 18, 20, and 23 are Evil.
2. Repeated Division-by-2 Trace for 9 and 23
Tracing repeated division by 2 illustrates why prepending each remainder builds the exact binary string from right to left. Take num = 9: temp starts at 9 and bin is empty. Step 1 computes 9 % 2 = 1, increments countOnes to 1, prepends "1" so bin = "1", and sets temp = 4. Step 2 yields 4 % 2 = 0, leaves countOnes at 1, sets bin = "01", and sets temp = 2. Step 3 gives 2 % 2 = 0, keeps countOnes at 1, sets bin = "001", and sets temp = 1. Step 4 evaluates 1 % 2 = 1, raises countOnes to 2, sets bin = "1001", and reduces temp to 0, ending the loop. Since countOnes % 2 == 0, 9 is Evil. Similarly, num = 23 produces remainders 1, 1, 1, 0, 1 (from right to left), yielding bin = "10111" and countOnes = 4, also Evil.
- Trace 9: 9->4 (rem 1), 4->2 (rem 0), 2->1 (rem 0), 1->0 (rem 1) -> "1001", two 1s.
- Trace 23: remainders 1, 1, 1, 0, 1 -> "10111", four 1s (Evil).
- Trace 14: remainders 0, 1, 1, 1 -> "1110", three 1s (Odious).
3. Handling Zero and Negative Input Bounds
Handling zero and negative inputs explicitly separates production-ready BlueJ classes from fragile textbook snippets. When num equals 0, the while (temp > 0) loop never enters; without the dedicated if (num == 0) branch, bin would print blank instead of "0", even though countOnes correctly stays 0 and 0 % 2 == 0 passes. Likewise, negative integers in Java use two's-complement representation, so passing -5 into temp % 2 and temp /= 2 skips the loop entirely if guarded by temp > 0. Adding the upfront num < 0 check stops invalid inputs cleanly before conversion begins. Notice also why storing the binary output in a String rather than an int is safer: even a modest decimal number like 1024 needs 11 decimal digits ("10000000000") if packed into a base-10 integer, quickly exceeding int limits.
- Explicitly set bin = "0" when num == 0 so binary output is never blank.
- Reject negative numbers upfront because Evil Numbers are defined on non-negative integers.
- Use String bin instead of int to prevent base-10 overflow on binary digits above 1023.
4. EvilNumber.java (BlueJ)
import java.util.Scanner;
public class EvilNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a non-negative integer: ");
int num = sc.nextInt();
if (num < 0) {
System.out.println("Invalid input: enter a non-negative integer.");
sc.close();
return;
}
int temp = num;
int countOnes = 0;
String bin = "";
if (num == 0) {
bin = "0";
} else {
while (temp > 0) {
int bit = temp % 2;
if (bit == 1) {
countOnes++;
}
bin = bit + bin;
temp /= 2;
}
}
System.out.println("Binary equivalent: " + bin);
System.out.println("Number of 1s: " + countOnes);
if (countOnes % 2 == 0) {
System.out.println(num + " is an Evil Number.");
} else {
System.out.println(num + " is not an Evil Number (Odious).");
}
sc.close();
}
}
Verified Console / Dry-Run Output:
Enter a non-negative integer: 9
Binary equivalent: 1001
Number of 1s: 2
9 is an Evil Number.
Enter a non-negative integer: 23
Binary equivalent: 10111
Number of 1s: 4
23 is an Evil Number.
5. Variable Description Table (VDT) — EvilNumber
| Variable Name | Data Type | Purpose in Program |
|---|---|---|
| sc | Scanner | Reads the non-negative integer input from the user |
| num | int | Stores the original decimal integer being tested |
| temp | int | Holds the quotient progressively divided by 2 |
| countOnes | int | Tallies how many 1-bits appear in the binary expansion |
| bit | int | Captures each binary remainder (0 or 1) via temp % 2 |
| bin | String | Assembles the binary equivalent in left-to-right order |
Decode Binary & Java Logic with 4Achievers Junior
Master binary base conversion, parity logic, and ISC practical coding in 1-on-1 or 1:4 Micro-Pods at 4Achievers Junior (Grades 3-12).
6. Common Mistakes & Top Scorer Tips
- Avoid Mistake: Appending bit after bin (bin = bin + bit), which prints the binary digits backward instead of prepending bit + bin.
- Avoid Mistake: Storing the binary representation inside an int or long variable, which overflows once num exceeds 1,023 (10-bit binary) or 524,287.
- Avoid Mistake: Skipping the num == 0 branch, which leaves bin as an empty string instead of "0".
- Examiner / Mentor Tip: Always print both the binary equivalent and the 1-bit count; ISC practical marking schemes often award separate marks for each output line.
- Examiner / Mentor Tip: Be ready to explain in viva why bin = bit + bin produces forward binary order without needing an array or StringBuilder.reverse().
- Examiner / Mentor Tip: Check your program with 0, 3, and 7 in BlueJ to verify both Evil and Odious branches plus the zero edge case.
7. Interactive Concept Check Quiz
Question: Which decimal integer below has an even number of 1s in binary and is therefore an Evil Number?
8. Frequently Asked Questions
Is 0 considered an Evil Number?
Does Java have a built-in method to count binary 1s?
Why do we write bin = bit + bin instead of bin += bit?
Explore Related Lessons & Free Interactive Tools
- All 110 Guides in ICSE Java BlueJ Library
- Fascinating Number Program in Java (ICSE/ISC)
- Smith Number Program in Java (Prime Factor Sum)
- Twin Prime Numbers Program in Java (Range Check)
- Interactive Tool: ICSE Class 10 Java BlueJ Output Quiz
- Interactive Tool: ICSE Variable Description Table (VDT) Auto-Formatter
