Neon Number in Java
A Neon Number is a special type of number in which the sum of the digits of its square is equal to the original number. It is a commonly asked question in Java programming assignments and coding interviews as it helps test loop logic, digit extraction, and mathematical thinking.
What Is a Neon Number?
A number n is called a Neon Number if:
Step 1 → Find square of n Step 2 → Sum the digits of the square If Sum == n → It is a Neon Number
Example
Consider the number 9:
- Square = 9 × 9 = 81
- Digit sum = 8 + 1 = 9
Since the digit sum equals the number, 9 is a Neon Number.
How Do We Check a Neon Number in Java?
Steps to check a Neon Number:
- Accept the number from the user
- Find its square
- Extract digits of the square one by one
- Add all the digits
- Compare the sum with the original number
Java Program to Check Neon Number
import java.util.*;
public class NeonNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int square = num * num;
int sum = 0;
while (square > 0) {
int digit = square % 10;
sum += digit;
square /= 10;
}
if (sum == num) {
System.out.println(num + " is a Neon Number.");
} else {
System.out.println(num + " is NOT a Neon Number.");
}
}
}
Example Walkthrough
Let's verify 9 step-by-step:
- Square = 81
- Digit extraction: 8, 1
- Sum = 8 + 1 = 9
Since the sum equals the original number, it is a Neon Number.
Known Neon Numbers
Only a few Neon Numbers exist:
- 0 → Square = 0, Sum = 0
- 1 → Square = 1, Sum = 1
- 9 → Square = 81, Sum = 9
These are the only Neon numbers in the positive integer range.
Practice Challenge
Write a Java program to find all Neon Numbers between 1 and 10,000. Loop through each number, apply the Neon logic, and print only the matching numbers.