Neon Number Program in Java BlueJ With Dry Run

⚑ Direct Answer & Key Takeaway

A Neon number is a non-negative integer n whose square n^2 has a digit sum equal to n itself. Remarkably, in base 10 only three Neon numbers exist: 0 (0^2 = 0), 1 (1^2 = 1), and 9 (9^2 = 81, 8 + 1 = 9). In Java BlueJ, compute `long square = (long) num * num;`, sum the digits of `square` (not `num`) inside `while (square > 0)`, and verify `sum == num`.

1. Why Only 0, 1, and 9 Are Base-10 Neon Numbers

Unlike infinite number families such as primes or palindromes, base-10 Neon numbers form a tiny finite set: only `0`, `1`, and `9`. By definition, an integer n >= 0 is Neon when the sum of the digits of n^2 equals n. Why can no integer >= 10 ever qualify? For any n-digit number, its square has at most 2d digits, so the digit sum of n^2 cannot exceed 9 * 2d = 18d. Meanwhile, the value of n grows exponentially as at least 10^(d - 1). By three digits (n >= 100), n^2 has at most 6 digits, capping its digit sum at 9 * 6 = 54β€”far below 100! Checking all 2-digit candidates up to 27 leaves 9 as the largest Neon number. Knowing this upper bound is a classic ICSE viva advantage when asked why loops scanning 1 to 1000 only print 1 and 9.

  • 0^2 = 0 (sum 0), 1^2 = 1 (sum 1), and 9^2 = 81 (sum 8 + 1 = 9) are the only base-10 Neon numbers.
  • Do not confuse 9 (Neon) with 81 (its square, whose square 6561 sums to 18 != 81).
  • For any integer >= 100, the maximum possible digit sum of n^2 is 54, making equality impossible.

2. Squaring First and Summing Square Digits in Java

Programming the Neon number check in Java reverses the usual target of digit extraction: instead of breaking down `num`, the `while` loop breaks down `square = (long) num * num`. Because `num` itself is never mutated inside the loop, there is no need for a separate `temp` copy of `num`β€”you only need `originalSquare` if you want to display the square in the output sentence. Pay close attention to operator precedence when extracting digits from `long square`: `(int) (square % 10)` evaluates the remainder `0..9` first and safely casts that single digit to `int`, whereas omitting parentheses (`(int) square % 10`) would truncate `square` first. When `num == 0`, the loop condition `square > 0` skips immediately, leaving `sum = 0`, which correctly equals `num`.

  • Extract digits from `square`, leaving `num` untouched for the final `sum == num` check.
  • Always wrap `square % 10` in parentheses before casting to `(int)`.
  • Guard `num < 0` upfront so negative squares do not falsely match or trigger confusion.

3. Complete Dry Run Trace for Input 9 and 12

Tracing `num = 9` and `num = 12` highlights both outcomes clearly. For `num = 9`, `num < 0` is false, so `square = 9 * 9 = 81`, `originalSquare = 81`, `digit = 0`, and `sum = 0`. In Iteration 1 (`81 > 0`), `digit = (int) (81 % 10) = 1`, `sum = 0 + 1 = 1`, and `square = 81 / 10 = 8`. In Iteration 2 (`8 > 0`), `digit = (int) (8 % 10) = 8`, `sum = 1 + 8 = 9`, and `square = 8 / 10 = 0`. Now `square > 0` is false; `sum == num` (`9 == 9`) is `true`, printing `9 is a Neon number (Square = 81, Digit Sum = 9).` For `num = 12`, `square = 144`: Pass 1 adds `4` (`square = 14`); Pass 2 adds `4` (`sum = 8, square = 1`); Pass 3 adds `1` (`sum = 9, square = 0`). Since `9 != 12`, it outputs `12 is NOT a Neon number`.

  • Input 9 trace: square=81 -> Pass 1: digit=1, sum=1, sq=8 -> Pass 2: digit=8, sum=9, sq=0 -> 9 == 9 (Neon).
  • Input 12 trace: square=144 -> digits 4, 4, 1 give sum=9 -> 9 != 12 (Not Neon).
  • Input 0 trace: square=0 -> loop skipped -> sum=0 == num=0 (Neon).

4. NeonNumber.java (ICSE BlueJ Program)

import java.util.Scanner;

public class NeonNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a non-negative integer: ");
        int num = sc.nextInt();

        if (num < 0) {
            System.out.println(num + " is NOT a Neon number.");
            sc.close();
            return;
        }

        long square = (long) num * num;
        long originalSquare = square;
        int digit = 0;
        int sum = 0;

        while (square > 0) {
            digit = (int) (square % 10);
            sum = sum + digit;
            square = square / 10;
        }

        if (sum == num) {
            System.out.println(num + " is a Neon number (Square = " + originalSquare + ", Digit Sum = " + sum + ").");
        } else {
            System.out.println(num + " is NOT a Neon number (Square = " + originalSquare + ", Digit Sum = " + sum + ").");
        }
        sc.close();
    }
}

Verified Console / Dry-Run Output:

Enter a non-negative integer: 9
9 is a Neon number (Square = 81, Digit Sum = 9).

Enter a non-negative integer: 12
12 is NOT a Neon number (Square = 144, Digit Sum = 9).

5. Variable Description Table (VDT)

Variable NameData TypePurpose in Program
scScannerReads the user-entered integer from standard input
numintStores the original integer compared against the square's digit sum
squarelongHolds num * num and is divided by 10 during digit summation
originalSquarelongPreserves the full square value for printing in the output message
digitintStores each extracted digit of square (square % 10)
sumintAccumulates the sum of all digits of square
🎯 1-on-1 Live Mentorship & Free Study Kit

Master Java Dry Runs & BlueJ Logic With 1-on-1 Help

Get guided BlueJ debugging, dry-run practice, and exam-ready VDT reviews from IIT/NIT educators at 4Achievers Junior (Grades 3-12, 1-on-1 & 1:4 Micro-Pods).

πŸ’¬ WhatsApp Now
πŸ”’ Instant WhatsApp notes + free 60-min 1-on-1 trial session
Confirmed! Our Senior Mentor is sending your study kit & free 1-on-1 demo link on WhatsApp now.

6. Common Mistakes & Top Scorer Tips

  • Avoid Mistake: Summing the digits of `num` instead of summing the digits of `square = num * num`, which accidentally turns the program into a single-digit check.
  • Avoid Mistake: Forgetting to preserve `originalSquare` when printing the square after the while loop reduces `square` down to `0`.
  • Avoid Mistake: Casting `(int) square % 10` without parentheses around `(square % 10)`β€”because `(int)` binds tighter than `%`, `(int) square % 10` truncates `square` to `int` BEFORE taking `% 10`!
  • Examiner / Mentor Tip: Remember the three Neon numbers (0, 1, 9) so you can immediately verify your output during practical exams.
  • Examiner / Mentor Tip: Explain clearly in your VDT that `square` is what gets divided by 10 inside the loop, not `num`.
  • Examiner / Mentor Tip: Show both `square` and `originalSquare` in your VDT if you display the square in the final print statement.

7. Interactive Concept Check Quiz

Question: Which of the following numbers is NOT a Neon number?

8. Frequently Asked Questions

Why are 0, 1, and 9 the only Neon numbers in base 10?
Every integer n >= 10 has at most floor(log10(n^2)) + 1 digits in its square, so its maximum possible digit sum is 9 * (2 * log10(n) + 1), which grows logarithmically and stays strictly smaller than n for all n >= 10 (even at n = 10, max digit sum of a 3-digit square is 27, and testing 10..27 yields none).
Does the BlueJ Neon number code work correctly for input 0?
Yes, `0` is a Neon number (`0 * 0 = 0`, digit sum `0 == 0`). Because `square > 0` is false for `0`, `sum` remains `0`, so `sum == num` evaluates to `true`.
Why are parentheses required in (int) (square % 10)?
Because `(int)` has higher operator precedence than `%`, writing `(int) square % 10` casts `square` to `int` first and can overflow on large inputs; writing `(int) (square % 10)` takes the single digit `0..9` first before casting.

Explore Related Lessons & Free Interactive Tools

Free 1-on-1 Session

Book a Free Demo Class