Introduction
A neon number is one of the lesser-known "special number" categories in Java programming exercises. Unlike more commonly discussed numbers such as Armstrong, perfect, and strong numbers, a neon number is built using a simple combination of squaring a number and summing the digits of the result.
If you've already worked through the other special-number programs in this series, checking for a neon number will feel refreshingly straightforward because it combines just two familiar operations:
- Squaring a number
- Extracting and summing digits
In this guide, you'll learn:
- How to check a neon number using a
whileloop - How to solve it using recursion
- How to print all neon numbers within a range
- How neon numbers differ from Armstrong and Strong numbers
What Is a Neon Number?
A neon number is a number whose square's digit sum equals the original number.
Formula
If:
Sum of digits of (number × number) = number
Then the number is called a Neon Number.
Examples
| Number | Square | Sum of Digits | Neon Number? |
|---|---|---|---|
| 0 | 0 | 0 | ✅ Yes |
| 1 | 1 | 1 | ✅ Yes |
| 9 | 81 | 8 + 1 = 9 | ✅ Yes |
| 5 | 25 | 2 + 5 = 7 | ❌ No |
For example:
9² = 81
8 + 1 = 9
Since the digit sum of the square equals the original number, 9 is a neon number.
Method 1: Using a While Loop
This is the standard and most commonly used approach.
Java Program
public class NeonNumberCheck {
public static void main(String[] args) {
int num = 9;
int square = num * num;
int sum = 0;
while (square != 0) {
int digit = square % 10;
sum = sum + digit;
square = square / 10;
}
if (sum == num) {
System.out.println(num + " is a neon number.");
} else {
System.out.println(num + " is not a neon number.");
}
}
}
Output
9 is a neon number.
Step-by-Step Execution
Suppose:
num = 9
Step 1
Calculate the square.
9 × 9 = 81
Now:
square = 81
sum = 0
Step 2
Extract the last digit.
digit = 81 % 10 = 1
Add it to the sum.
sum = 1
Remove the last digit.
square = 81 / 10 = 8
Step 3
Extract the next digit.
digit = 8 % 10 = 8
Add it.
sum = 1 + 8 = 9
Update square.
square = 8 / 10 = 0
The loop ends.
Step 4
Compare:
sum = 9
num = 9
Since both are equal,
9 is a Neon Number.
Why Use a Separate square Variable?
Unlike palindrome or Armstrong programs, we don't modify the original number.
Instead:
- Calculate the square.
- Store it in a separate variable.
- Sum the digits of the square.
- Compare the final sum with the original number.
This keeps the original input unchanged throughout the program.
Time Complexity
- Time Complexity: O(d)
- Space Complexity: O(1)
where d is the number of digits in the squared value.
Method 2: Using Recursion
Instead of using a loop to sum the digits, we can write a recursive function.
The recursive function repeatedly:
- extracts the last digit,
- adds it,
- calls itself with the remaining digits.
Java Program
public class NeonNumberRecursion {
static int sumOfDigits(int n) {
if (n == 0) {
return 0;
}
return (n % 10) + sumOfDigits(n / 10);
}
public static void main(String[] args) {
int num = 9;
int square = num * num;
int sum = sumOfDigits(square);
if (sum == num) {
System.out.println(num + " is a neon number.");
} else {
System.out.println(num + " is not a neon number.");
}
}
}
Output
9 is a neon number.
How the Recursion Works
For:
square = 81
The recursive calls unfold like this:
sumOfDigits(81)
= (81 % 10) + sumOfDigits(8)
= 1 + sumOfDigits(8)
= 1 + 8 + sumOfDigits(0)
= 1 + 8 + 0
= 9
The recursion stops when:
n == 0
which acts as the base case.
Reusing Existing Logic
If you've already implemented a Sum of Digits program, you can reuse the same recursive method here.
The only difference is that instead of passing the original number, you pass:
num * num
This is a good example of code reuse and modular programming.
Time Complexity
- Time Complexity: O(d)
- Space Complexity: O(d)
where d is the number of digits in the squared value because each recursive call occupies one stack frame.
Method 3: Printing All Neon Numbers in a Range
Instead of checking just one number, we can extend the program to find every neon number within a given range.
The best approach is to place the neon number logic inside a reusable isNeon() method and call it for each number in the range.
Java Program
public class NeonNumbersInRange {
static boolean isNeon(int num) {
int square = num * num;
int sum = 0;
while (square != 0) {
sum += square % 10;
square /= 10;
}
return sum == num;
}
public static void main(String[] args) {
int start = 0;
int end = 100;
System.out.println("Neon numbers between " + start + " and " + end + ":");
for (int num = start; num <= end; num++) {
if (isNeon(num)) {
System.out.print(num + " ");
}
}
}
}
Output
Neon numbers between 0 and 100:
0 1 9
How It Works
The program follows these steps:
- Iterate through every number between
startandend. - Call the reusable
isNeon()method. - Square the current number.
- Calculate the sum of the digits of the square.
- Compare the digit sum with the original number.
- Print the number if both values are equal.
This approach keeps the checking logic separate from the looping logic, making the code easier to reuse and maintain.
Why Are Neon Numbers So Rare?
Only the following numbers satisfy the neon condition between 0 and 100:
- 0
- 1
- 9
As numbers become larger, their squares increase much more rapidly.
For example:
20² = 400
The digit sum becomes:
4 + 0 + 0 = 4
which is nowhere near 20.
Because a number grows linearly while its square grows quadratically, it becomes increasingly unlikely for the digit sum of the square to equal the original number.
Time Complexity
If:
- n = number of values in the range
- d = number of digits in the squared value
then:
- Time Complexity: O(n × d)
- Space Complexity: O(1)
Neon Numbers vs Armstrong Numbers vs Strong Numbers
These three special-number problems often look similar because they all involve processing digits.
However, each one follows a completely different mathematical rule.
| Type | Rule | Example |
|---|---|---|
| Neon Number | Sum of digits of the square equals the original number | 9 → 81 → 8 + 1 = 9 |
| Armstrong Number | Sum of each digit raised to the power of the number of digits equals the original number | 153 → 1³ + 5³ + 3³ = 153 |
| Strong Number | Sum of the factorials of each digit equals the original number | 145 → 1! + 4! + 5! = 145 |
Neon Number
For a neon number:
- Square the number.
- Sum the digits of the square.
- Compare the result with the original number.
Example:
9² = 81
8 + 1 = 9
Armstrong Number
For an Armstrong number:
- Count the number of digits.
- Raise each digit to that power.
- Add all the powered digits.
- Compare the sum with the original number.
Example:
153
= 1³ + 5³ + 3³
= 1 + 125 + 27
= 153
Strong Number
For a strong number:
- Find the factorial of every digit.
- Add the factorials.
- Compare the sum with the original number.
Example:
145
= 1! + 4! + 5!
= 1 + 24 + 120
= 145
Key Difference
Although all three programs process digits, they perform completely different operations.
| Number Type | Main Operation |
|---|---|
| Neon | Square the entire number, then sum its digits |
| Armstrong | Raise each digit to a power |
| Strong | Find the factorial of each digit |
Understanding this distinction helps prevent one of the most common interview mistakes—confusing one special-number problem with another.
How Java Handles This Internally (Memory Concept)
Methods 1 and 3
In the iterative solutions:
numsquaresumdigit
are all primitive int variables.
These variables are stored in stack memory.
The statement:
int square = num * num;
performs a simple arithmetic multiplication directly on primitive values.
No heap objects are created during the calculation.
Method 2
The recursive solution works differently.
Each call to:
sumOfDigits(n)
creates a new stack frame.
For example:
sumOfDigits(81)
↓
sumOfDigits(8)
↓
sumOfDigits(0)
As each recursive call returns, Java combines the results to produce the final digit sum.
Because every recursive call consumes stack memory, very large inputs may eventually lead to:
StackOverflowError
although this is unlikely for typical neon number programs because squared values usually contain relatively few digits.
Integer Overflow While Squaring
One important point is that squaring can quickly produce much larger numbers.
For example:
50000 × 50000
produces:
2,500,000,000
which exceeds the maximum value that can be stored in an int.
For larger inputs, it is safer to use:
long square = (long) num * num;
This ensures the multiplication is performed using 64-bit arithmetic.
Real-Life Analogy: A Number Whose Reflection Adds Back Up
Imagine standing in front of a magical mirror.
Instead of showing your normal reflection, the mirror exaggerates everything by squaring your appearance.
Now imagine counting every small feature in that exaggerated reflection.
Surprisingly, when you add all those individual features together, they exactly equal your original height.
That would be an unusual coincidence.
A neon number behaves in much the same way.
- The original number is reflected as its square.
- The square is broken into individual digits.
- Those digits are added together.
- Somehow, they recreate the original number.
This rare mathematical coincidence is what makes neon numbers interesting despite their simple definition.
Comparison of All Methods
| Method | Heap Memory Used? | Time Complexity | Best Used When |
|---|---|---|---|
| While Loop | No | O(d) | Standard solution, interviews, and production code |
| Recursion | No (uses call stack) | O(d) | Demonstrating recursive thinking or reusing an existing recursive digit-sum method |
| Range-Based Loop | No | O(n × d) | Finding every neon number within a range |
Note:
- d = Number of digits in the squared value.
- n = Total numbers in the specified range.
Best Practices
- Use a reusable method such as
isNeon(int num)instead of writing the checking logic directly insidemain(). This improves readability and promotes code reuse. -
If you're working with larger numbers, store the squared value in a
longinstead of anintto reduce the risk of integer overflow.long square = (long) num * num; - If you already have a tested sum of digits utility method, reuse it instead of writing another digit-summing loop. Neon number logic naturally builds upon this common utility.
- Remember that neon numbers are relatively rare, so don't expect many results even when searching large ranges.
- Clearly distinguish between Neon, Armstrong, and Strong numbers when writing documentation or explaining your solution during interviews, as these concepts are often confused.
- Prefer arithmetic operations (
%and/) over converting numbers to strings, since numeric operations are generally more efficient and better demonstrate understanding of digit manipulation.
Common Mistakes Beginners Make
1. Forgetting to Square the Number
A common mistake is summing the digits of the original number instead of its square.
Incorrect approach:
int sum = sumOfDigits(num);
Correct approach:
int square = num * num;
int sum = sumOfDigits(square);
2. Confusing Neon Numbers with Armstrong Numbers
Many beginners accidentally apply the Armstrong number logic by raising digits to powers.
Remember:
- Neon Number: Square the entire number first, then sum the digits.
- Armstrong Number: Raise each digit to a specific power before summing.
These are completely different algorithms.
3. Ignoring Integer Overflow
For sufficiently large values of num, the calculation:
num * num
may overflow the range of an int.
To avoid this, write:
long square = (long) num * num;
4. Assuming Neon Numbers Are Common
Unlike palindrome numbers, neon numbers are extremely uncommon.
For example, between 0 and 100, only:
- 0
- 1
- 9
are neon numbers.
5. Using String Conversion Unnecessarily
Some beginners convert the squared value into a string and then iterate through each character.
Although this works, the arithmetic approach is simpler and more efficient:
digit = square % 10;
square /= 10;
Expert Tips for Interviews
A strong interview answer might sound like this:
"A neon number is a number whose square has a digit sum equal to the original number. My approach is to first calculate the square, then repeatedly extract and add its digits using modulus and division. Finally, I compare the digit sum with the original number. Unlike an Armstrong number, which raises each digit to a power, or a Strong number, which sums the factorials of each digit, a neon number specifically works with the digits of the squared value."
Mentioning the differences between these related special-number problems shows a clear understanding of the underlying concepts rather than simply memorizing algorithms.
Pros and Cons
Using a While Loop
Pros
- ✅ Simple and easy to understand
- ✅ Efficient with constant space usage
- ✅ No recursion or additional memory allocation
- ✅ Commonly expected in interviews
Cons
- ❌ Requires care when squaring larger numbers due to possible integer overflow
Using Recursion
Pros
- ✅ Demonstrates recursive problem-solving
- ✅ Reuses an existing recursive digit-sum method
- ✅ Produces clean and modular code
Cons
- ❌ Uses additional call stack memory
- ❌ Slightly slower than the iterative approach because of recursive function calls
- ❌ Not ideal for unnecessarily large recursive depths
Using a Range-Based Approach
Pros
- ✅ Easily finds every neon number within a specified range
- ✅ Reuses the
isNeon()method - ✅ Simple to extend for counting or storing neon numbers
Cons
- ❌ Requires checking every number in the range
- ❌ Results are usually very small because neon numbers are rare
Frequently Asked Questions
1. What is a neon number?
A neon number is a number whose square's digit sum equals the original number.
For example:
9² = 81
8 + 1 = 9
Therefore, 9 is a neon number.
2. What are some examples of neon numbers?
Between 0 and 100, the neon numbers are:
- 0
- 1
- 9
3. How do I check whether a number is a neon number in Java?
Follow these steps:
- Calculate the square of the number.
- Extract every digit of the square.
- Add the digits together.
- Compare the sum with the original number.
If both values are equal, the number is a neon number.
4. What is the difference between a neon number and an Armstrong number?
A neon number sums the digits of the square of the number.
An Armstrong number sums each digit of the original number, where each digit is raised to the power of the total number of digits.
5. What is the difference between a neon number and a strong number?
A neon number works with the digits of the square.
A strong number calculates the factorial of every digit in the original number before summing them.
6. Can I check a neon number using recursion?
Yes.
Calculate the square first, then use a recursive function to calculate the sum of the digits of the squared value.
7. Are neon numbers common?
No.
They are relatively rare.
Within the range 0 to 100, only three numbers satisfy the neon number condition.
8. Is 0 considered a neon number?
Yes.
0² = 0
Digit sum = 0
Since the digit sum equals the original number, 0 is a neon number.
9. What is the time complexity of checking a neon number?
The algorithm processes each digit of the squared value exactly once.
- Time Complexity: O(d)
where d is the number of digits in the squared number.
10. Can squaring a number cause integer overflow?
Yes.
For sufficiently large values, num × num may exceed the range of an int.
Using long is recommended when working with larger inputs.
11. Is checking for a neon number a common interview question?
It is less common than questions on Armstrong, palindrome, or prime numbers, but it occasionally appears to test a candidate's understanding of digit manipulation and reusable programming logic.
12. How do I print all neon numbers within a range?
Loop through every number in the specified range and call a reusable isNeon() method.
Print each number that satisfies the neon number condition.