Prashnikaप्रश्निका
Code
About
‹ 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

  1. Accept the number num. If num < 0, display Invalid input and stop.
  2. Find the square sq = (long) num * num.
  3. Copy sq into n and set s = 0. While n > 0, add the last digit n % 10 to s and remove it with n = n / 10.
  4. If s == num, display that num is 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

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the input typed by the user
numintThe number entered by the user
sqlongThe square of num
nlongCopy of sq whose last digit is removed in each pass
sintSum of the digits of the square

Sample input and output

Sample 1

Enter a number: 9
9 is a neon number

Sample 2

Enter a number: 12
12 is not a neon number

Sample 3

Enter a number: 1
1 is a neon number

Sample 4

Enter a number: -5
Invalid input

One value per line

Dry run

Input: num = 9, so sq = 81 and its digits are added.

Stepnn % 10s
Start81-0
1811
2089

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

  1. Range: "Display all neon numbers between m and n."
  2. Below a limit: "Display all neon numbers from 0 to 10000."
  3. Recursive technique: "Check whether a number is a neon number using a recursive method to find the digit sum."