Special Number Programs in Java Explained
Special Numbers in Java
Armstrong numbers, perfect numbers, Harshad numbers, neon numbers — Java courses and coding tests are full of "is this a ___ number?" questions. They look like dozens of separate puzzles, but they're built from only a handful of techniques: pulling digits out of a number, adding up divisors, testing for primes, and repeating a step until a pattern appears. Learn those techniques once and every special-number program becomes a short combination of them.
The Core Toolkit: Working with Digits
Most special numbers are defined by their digits. Two operators do almost all the work:
- ›
n % 10gives the last digit. - ›
n / 10removes the last digit (integer division drops the remainder).
Repeating those two steps in a loop visits every digit from right to left:
1public class DigitToolkit {
2 public static void main(String[] args) {
3 int n = 4729;
4
5 int sum = 0, product = 1, count = 0;
6 for (int t = n; t > 0; t /= 10) { // work on a copy so n stays unchanged
7 int digit = t % 10;
8 sum += digit;
9 product *= digit;
10 count++;
11 }
12
13 int reversed = 0;
14 for (int t = n; t > 0; t /= 10) {
15 reversed = reversed * 10 + t % 10; // shift left, append the digit
16 }
17
18 System.out.println("Digits: " + count);
19 System.out.println("Sum: " + sum + ", product: " + product);
20 System.out.println("Reversed: " + reversed);
21 }
22}Output:
Digits: 4
Sum: 22, product: 504
Reversed: 9274
Always loop over a copy (t) of the number. Many special-number checks compare the result with the original value, and the loop would otherwise have reduced it to 0.
Family 1: Numbers Defined by Digit Powers and Factorials
These numbers equal a sum built from their own digits.
| Number | Rule | Example |
|---|---|---|
| Armstrong | Sum of each digit raised to the power of the digit count | 153 = 1³ + 5³ + 3³ |
| Disarium | Sum of each digit raised to its position (1, 2, 3 … from the left) | 89 = 8¹ + 9² |
| Strong | Sum of the factorials of the digits | 145 = 1! + 4! + 5! |
The pattern is the same for all three: extract each digit, transform it (a power or a factorial), add the results, and compare the total with the original number. The only difference is the transformation — and for Disarium, the position must be counted from the left, so it's easiest to work with the digit count or the number as a string.
The four 3-digit Armstrong numbers are 153, 370, 371 and 407.
Family 2: Numbers Defined by Digit Sums and Products
| Number | Rule | Example |
|---|---|---|
| Harshad (Niven) | Divisible by the sum of its digits | 18: digit sum 9, and 18 ÷ 9 = 2 |
| Neon | The digit sum of its square equals the number | 9² = 81, and 8 + 1 = 9 |
| Spy | Digit sum equals digit product | 1124: 1+1+2+4 = 8 and 1×1×2×4 = 8 |
| Magic | Repeated digit sums end at 1 | 19 → 10 → 1 |
| Duck | Contains a zero digit (not at the start) | 1023 |
These only need the digit loop from the toolkit, sometimes applied to a derived value such as the square (Neon) or repeated until one digit remains (Magic).
Family 3: Numbers Defined by Divisors
A proper divisor of n is any divisor smaller than n itself. Comparing the sum of proper divisors with n sorts every number into one of three groups:
| Number | Rule | Example |
|---|---|---|
| Perfect | Sum of proper divisors equals n | 28 = 1 + 2 + 4 + 7 + 14 |
| Abundant | Sum is greater than n | 12: 1+2+3+4+6 = 16 |
| Deficient | Sum is less than n | 8: 1+2+4 = 7 |
The simple way loops i from 1 to n - 1. The faster way loops only up to √n and adds both i and n / i whenever i divides n, because divisors come in pairs — taking care not to add the square root twice, or n itself.
Family 4: Numbers Built on Primes
| Number | Rule | Example |
|---|---|---|
| Twin primes | Two primes that differ by 2 | 41 and 43 |
| Co-prime | Two numbers whose GCD is 1 | 8 and 15 |
| Circular prime | Every rotation of its digits is prime | 197, 971, 719 |
| Emirp | Prime whose reversed digits form a different prime | 13 and 31 |
Each one starts from a single helper — a prime test that tries divisors up to √n — combined with a second step: comparing two primes, computing a GCD, rotating digits, or reversing them. Writing isPrime once as its own method keeps all four programs short.
Family 5: Numbers That Reproduce Themselves
| Number | Rule | Example |
|---|---|---|
| Automorphic | Its square ends with the number itself | 76² = 5776 |
| Kaprekar | Split its square into two parts that add back to the number | 45² = 2025, and 20 + 25 = 45 |
| Keith | Appears in a Fibonacci-like sequence seeded by its digits | 14: 1, 4, 5, 9, 14 |
| Happy | Repeatedly summing the squares of its digits reaches 1 | 19 → 82 → 68 → 100 → 1 |
| Palindromic | Reads the same reversed | 12321 |
For the "repeat until" checks, such as Happy and Magic, make sure the loop can end. Unhappy numbers never reach 1 — they fall into a cycle that always passes through 4 — so the loop must stop on 1 or on 4 (or track the values it has already seen).
Family 6: Numbers Defined in Binary
| Number | Rule | Example |
|---|---|---|
| Evil | Even number of 1 bits in binary | 5 = 101 (two 1s) |
| Odious | Odd number of 1 bits in binary | 7 = 111 (three 1s) |
Integer.bitCount(n) counts the 1 bits directly. Without it, use n % 2 and n / 2 — the binary version of the digit loop above.
Common Mistakes
Mistake 1 — Destroying the Original Number
Running while (n > 0) { ...; n /= 10; } and then comparing the result with n always compares against 0. Copy the number into a separate variable before taking it apart.
Mistake 2 — Using Math.pow for Integer Powers
Math.pow returns a double. For large values the result can be off by a tiny rounding error, and an equality check then fails. Multiply in a small loop, or cast with rounding: (int) Math.round(Math.pow(d, k)).
Mistake 3 — Infinite Loops in Repeated Checks
A happy-number check written as while (n != 1) never ends for an unhappy number. Every "repeat until" check needs a second exit condition for the case where the target is never reached.
Mistake 4 — Counting n as Its Own Divisor
Perfect, abundant and deficient numbers use proper divisors only. Including n itself makes every number look abundant.
Interview Questions
How do you count the digits of a number without converting it to a string?
Divide by 10 in a loop and count the iterations, treating 0 as one digit. Alternatively, (int) Math.log10(n) + 1 works for positive numbers.
What's the difference between an Armstrong number and a Disarium number?
Armstrong raises every digit to the same power — the digit count. Disarium raises each digit to its own position from the left, so the power changes from digit to digit.
Why are prime checks done only up to the square root?
If n had a factor larger than √n, the matching factor would be smaller than √n and would already have been found. Stopping at √n turns an O(n) check into O(√n).
FAQs
Are these special numbers used in real software?
Rarely as-is, but the techniques are everywhere: digit extraction appears in checksums such as credit-card and ISBN validation, divisor sums in number theory, and bit counting in compression and cryptography.
Which special numbers are asked most often?
Armstrong, perfect, prime, palindrome and Fibonacci numbers come up most, followed by strong, Harshad, neon and happy numbers.
Should I use int or long?
int is fine for the examples here. Squares and powers grow fast, though — Neon, Automorphic and Kaprekar checks square the number, so use long once inputs go beyond about 46,000.
Summary
Special-number questions reduce to a few reusable techniques: extract digits with % 10 and / 10, sum proper divisors, test primality up to √n, repeat a transformation until it reaches a target or cycles, and count bits in binary. Each family above is one of those techniques plus a comparison. Use the practice programs below to try each one.
What to Read Next
Practice Programs
Put this topic into practice — each program has full code, output and a step-by-step explanation you can run online.
Determines whether a number equals the sum of its own digits raised to the digit count.
Determines whether a number equals the sum of its own proper divisors.
Determines whether a number equals the sum of the factorials of its own digits.
Determines whether a number is exactly divisible by the sum of its own digits.
Determines whether the sum of the digits of a number's square equals the number itself.
Determines whether the sum of a number's digits equals the product of its digits.
Determines whether repeatedly summing the squares of a number's digits eventually reaches 1.
Determines whether a number reads the same forwards and backwards.
Determines whether a number appears as the trailing digits of its own square.
Determines whether the sum of a number's digits, each raised to its own position, equals the number itself.
Show all 38 practice programs ↓Show fewer ↑
Determines whether a number is a magic number.
Determines whether a number contains a zero digit anywhere in it.
Determines whether a number's proper divisors add up to more than the number itself.
Determines whether a number's proper divisors add up to less than the number itself.
Determines whether two numbers form a twin prime pair.
Determines whether two numbers share no common factor besides 1.
Determines whether every digit-rotation of a number is also prime.
Determines whether a number is prime, and its digit-reversal is a different prime.
Determines whether a number is a Kaprekar number.
Determines whether a number reappears in a Fibonacci-like sequence seeded by its own digits.
Determines whether a number's binary representation has an even number of 1 bits.
Determines whether a number's binary representation has an odd number of 1 bits.
Determines whether a given number is itself a Fibonacci number.
Lists every Armstrong number within a given range.
Lists every perfect number within a range.
Lists every strong number within a given range.
Lists every happy number within a given range.
Lists every automorphic number within a given range.
Lists every twin prime pair up to a given limit.
Lists every prime number between a lower and upper bound.
Counts how many prime numbers fall between a lower and upper bound.
Adds up every prime number within a range.
Breaks a number down into its full product of prime factors.
Finds the biggest prime number that divides a number evenly.
Finds the smallest prime number that divides a number evenly.
Finds the greatest common divisor of two numbers.
Finds the least common multiple of two numbers.
Reduces a number down to a single digit by repeatedly summing its digits.