‹ All Computer Science notes
ISC Practicals and Theory - Neon Number
Updated 21 Oct 2026
A neon number is a number whose digits, when the number is squared and the digits of the square are added, give back the number itself. This note gives the program written in the main method alone: it squares the number, adds the digits of the square and compares the sum with the number.
Definition
A number $N$ is a neon number if the sum of the digits of $N^2$ is equal to $N$.
- $9$ is a neon number: $9^2 = 81$ and $8 + 1 = 9$.
- $12$ is not a neon number: $12^2 = 144$ and $1 + 4 + 4 = 9$, which is not $12$.
- $0$ and $1$ are neon numbers: $0^2 = 0$ and $1^2 = 1$.
Algorithm
- Accept the number
num. Ifnum < 0, displayInvalid inputand stop. - Find the square
sq = (long) num * num. - Copy
sqintonand sets = 0. Whilen > 0, add the last digitn % 10tosand remove it withn = n / 10. - If
s == num, display thatnumis a neon number. Otherwise display that it is not.
Program
Everything is written inside main: the square, the loop that adds the digits of the square, and the comparison.
import java.util.Scanner;
public class Neon
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt(); // the number to be checked
if (num < 0)
{
System.out.println("Invalid input");
}
else
{
long sq = (long) num * num; // the square of num
long n = sq; // copy of sq whose digits are added
int s = 0; // sum of the digits of the square
while (n > 0)
{
s = s + (int) (n % 10);
n = n / 10;
}
if (s == num)
System.out.println(num + " is a neon number");
else
System.out.println(num + " is not a neon number");
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
num | int | The number entered by the user |
sq | long | The square of num |
n | long | Copy of sq whose last digit is removed in each pass |
s | int | Sum of the digits of the square |
Sample input and output
Sample 1
Enter a number: 9
9 is a neon numberSample 2
Enter a number: 12
12 is not a neon numberSample 3
Enter a number: 1
1 is a neon numberSample 4
Enter a number: -5
Invalid inputDry run
Input: num = 9, so sq = 81 and its digits are added.
| Step | n | n % 10 | s |
|---|---|---|---|
| Start | 81 | - | 0 |
| 1 | 8 | 1 | 1 |
| 2 | 0 | 8 | 9 |
The loop stops because n is $0$. s is $9$, which is equal to num, so the program shows that $9$ is a neon number, as in the first sample run.
Variations
- Range: "Display all neon numbers between m and n."
- Below a limit: "Display all neon numbers from 0 to 10000."
- Recursive technique: "Check whether a number is a neon number using a recursive method to find the digit sum."