beginnerNumber ProblemsJava

Check Whether a Number Is a Neon Number

Determine whether the digit sum of a number's square equals the number.

Quick Answer

A neon number is one where the sum of the digits of its square equals the number itself. Square the number, add up the digits of the square, and compare with the original. For 9, 9^2 = 81 and 8 + 1 = 9, so 9 is a neon number.

Problem Statement

A neon number is a number whose square has digits that sum back to the number itself. For example, 9 is a neon number because 9^2 = 81 and 8 + 1 = 9.

Given a non-negative integer n, return true if it is a neon number and false otherwise. Square the number, sum the digits of that square, and compare the digit sum with the original n.

Input: A single non-negative integer n.

Output: true if n is a neon number, otherwise false.

Examples

Example 1
Input:  9
Output: true

9 squared is 81, and 8 + 1 = 9, which equals the original number.

Example 2
Input:  7
Output: false

7 squared is 49, and 4 + 9 = 13, which is not 7.

Constraints

  • 0 <= n; use an int square for small n (the only single-digit neon number is 9)
  • Sum the digits of the square, not of n

Think Before You Code

Reveal the questions to ask yourself first
  • Which number's digits do you sum — the original or its square?
  • How do you extract and add the digits of the square arithmetically?
  • What is the special case when n is 0?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
First square the number; the digits you sum come from the square, not from n.
Hint 2
Sum the digits of the square with the usual `% 10` and `/ 10` loop.
Hint 3
Compare that digit sum against the original n to decide.

Approach

Reveal the step-by-step approach

Square, sum the square's digits, then compare.

  1. Compute square = n * n.
  2. Set sum = 0.
  3. While square is not 0:
    • sum += square % 10 — add the last digit.
    • square = square / 10 — drop that digit.
  4. Return whether sum equals the original n.

Dry Run

Walk through the example step by step

Checking n = 9:

square | digit = square%10 | sum
-------+-------------------+----
81     | 1                 | 1
8      | 8                 | 9
0      | —                 | 9

digit sum (9) == original (9) -> true.

Solution

Reveal the full Java solution
public class NeonNumber {
    public static boolean isNeon(int n) {
        int square = n * n;
        int sum = 0;
        while (square != 0) {
            sum += square % 10;
            square /= 10;
        }
        return sum == n;
    }

    public static void main(String[] args) {
        System.out.println(isNeon(9)); // true
        System.out.println(isNeon(7)); // false
    }
}

The check hinges on summing the digits of the square rather than of the number itself. The digit-summing loop is the standard % 10 / / 10 pattern applied to square. For n = 0, square is 0, the loop is skipped, sum stays 0, and 0 == 0 correctly reports 0 as neon.

Time: O(d) where d is the number of digits in the squareSpace: O(1)

Common Mistakes

  • Summing the digits of n instead of the digits of its square.
  • Overflowing the square for large n by using int; use long for the square when n is big.

Edge Cases to Test

  • 0 is a neon number: 0^2 = 0 and the digit sum 0 equals 0.
  • 1 is a neon number: 1^2 = 1 and the digit sum 1 equals 1.

Interview Follow-Ups

  • How would you list every neon number below a given limit?
  • Why are 0, 1 and 9 the only neon numbers, and how would you argue that?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us