ISC Practicals and Theory - Evil Numbers in a Range
Updated 18 Oct 2026
An Evil Number is a positive whole number that has an even number of 1s in its binary (base-2) form. This version accepts a range m to n and prints every Evil Number in it, followed by how many there are.
Definition
A positive integer $N$ is an Evil Number if the count of 1s in its binary equivalent is even. For example, $6 = 110_2$ has two 1s, so 6 is Evil. $7 = 111_2$ has three 1s, so 7 is not Evil (it is an Odious Number). Between 1 and 20 the Evil Numbers are 3, 5, 6, 9, 10, 12, 15, 17, 18 and 20.
Algorithm
accept()reads the two ends of the range intomandn.- In
main, ifm <= 0,n <= 0orm > n, printINVALID INPUTand stop. - Otherwise
showEvil()setstotal = 0and runs a loop withifrommton:- Call
isEvil(i). It counts the 1s in the binary form ofiby repeated division by 2: whilex > 0, add 1 toonesifx % 2 == 1, thenx = x / 2. It returnstruewhenonesis even. - If it returns
true, printifollowed by a space and add 1 tototal.
- Call
- After the loop, print a new line and then the frequency,
total.
Program
accept() reads the range, isEvil() decides for one number, and showEvil() goes through the range, printing and counting.
import java.util.Scanner;
public class EvilRange
{
int m; // the start of the range
int n; // the end of the range
int total; // how many Evil Numbers were found in the range
// Step 1: read both ends of the range
public void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter m: ");
m = sc.nextInt();
System.out.print("Enter n: ");
n = sc.nextInt();
}
// Decides whether one number is Evil. Each remainder (x % 2) is the next
// binary digit, so we count the remainders that are 1 and check that
// the count is even.
public boolean isEvil(int x)
{
int ones = 0;
while (x > 0)
{
if (x % 2 == 1) // this binary digit is a 1
ones++;
x = x / 2; // drop that digit and move to the next one
}
return ones % 2 == 0;
}
// Print every Evil Number from m to n on one line and count them
public void showEvil()
{
total = 0;
System.out.println("Evil Numbers from " + m + " to " + n + ":");
for (int i = m; i <= n; i++)
{
if (isEvil(i))
{
System.out.print(i + " ");
total++;
}
}
System.out.println();
System.out.println("Frequency of Evil Numbers = " + total);
}
public static void main(String args[])
{
EvilRange ob = new EvilRange();
ob.accept();
if (ob.m <= 0 || ob.n <= 0 || ob.m > ob.n)
System.out.println("INVALID INPUT");
else
ob.showEvil();
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
| m | int | the start of the range, entered by the user |
| n | int | the end of the range, entered by the user |
| total | int | counts how many Evil Numbers were found in the range |
| sc | Scanner | reads the two numbers typed by the user |
| x | int | parameter of isEvil(); the number being tested, divided by 2 on each pass of its loop |
| ones | int | counts the 1s in the binary form of x |
| i | int | loop counter in showEvil(); each number from m to n in turn |
| args | String[] | command-line arguments of main (unused) |
| ob | EvilRange | object used to call accept() and showEvil() |
Sample input and output
Sample 1
Enter m: 1
Enter n: 20
Evil Numbers from 1 to 20:
3 5 6 9 10 12 15 17 18 20
Frequency of Evil Numbers = 10Sample 2
Enter m: 5
Enter n: 8
Evil Numbers from 5 to 8:
5 6
Frequency of Evil Numbers = 2Sample 3
Enter m: 7
Enter n: 8
Evil Numbers from 7 to 8:
Frequency of Evil Numbers = 0Sample 4
Enter m: 20
Enter n: 10
INVALID INPUTDry run
Input: m = 5, n = 8
| i | Binary of i | ones | isEvil(i) | Printed | total |
|---|---|---|---|---|---|
| 5 | 101 | 2 | true | 5 | 1 |
| 6 | 110 | 2 | true | 6 | 2 |
| 7 | 111 | 3 | false | — | 2 |
| 8 | 1000 | 1 | false | — | 2 |
For i = 5, isEvil(5) works as follows:
| Step | x | x % 2 | ones | x after x / 2 |
|---|---|---|---|---|
| Start | 5 | — | 0 | — |
| 1 | 5 | 1 | 1 | 2 |
| 2 | 2 | 0 | 1 | 1 |
| 3 | 1 | 1 | 2 | 0 |
The loop ends (x = 0) and ones = 2 is even, so it returns true. After the loop ends at i = 9, the program prints Frequency of Evil Numbers = 2.
For m = 20, n = 10 the check in main finds m > n, so showEvil() is never called and INVALID INPUT is printed.
Variations
- Odious Numbers in a range: "Accept two numbers m and n and print all Odious Numbers between them, that is, numbers with an odd number of 1s in binary."
- Both classifications: "Accept a range and print the Evil Numbers and the Odious Numbers in it separately, with the frequency of each."
- Binary equivalent: "Accept a range and print each Evil Number in it together with its binary equivalent."