Neon Number: flowchart, algorithm and program

This program reads a number n, squares it, and adds up the digits of the square. If that digit sum is equal to n, then n is a Neon number, like 9, since 9² = 81 and 8 + 1 = 9.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Set sum = 0.
  3. Step 3: Read n.
  4. Step 4: Set square = n * n.
  5. Step 5: Set temp = square.
  6. Step 6: If temp > 0, go to step 7; otherwise go to step 10.
  7. Step 7: Set digit = temp % 10.
  8. Step 8: Set sum = sum + digit.
  9. Step 9: Set temp = temp / 10. Go to step 6.
  10. Step 10: If sum == n, go to step 11; otherwise go to step 12.
  11. Step 11: Print n is a Neon number. Go to step 13.
  12. Step 12: Print n is not a Neon number.
  13. Step 13: Stop.

Pseudocode

BEGIN
    sum ← 0
    READ n
    square ← n * n
    temp ← square
    WHILE temp > 0 DO
        digit ← temp % 10
        sum ← sum + digit
        temp ← temp / 10
    END WHILE
    IF sum == n THEN
        PRINT n is a Neon number
    ELSE
        PRINT n is not a Neon number
    END IF
END

Program in C, Python and Java

Neon Number program in C

#include <stdio.h>

int main() {
    int n, square, temp, digit, sum = 0;
    printf("Enter n: ");
    scanf("%d", &n);
    square = n * n;
    temp = square;
    while (temp > 0) {
        digit = temp % 10;
        sum = sum + digit;
        temp = temp / 10;
    }
    if (sum == n)
        printf("%d is a Neon number\n", n);
    else
        printf("%d is not a Neon number\n", n);
    return 0;
}

Neon Number program in Python

n = int(input("Enter n: "))
square = n * n
temp = square
total = 0
while temp > 0:
    digit = temp % 10
    total = total + digit
    temp = temp // 10
if total == n:
    print(n, "is a Neon number")
else:
    print(n, "is not a Neon number")

Neon Number program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter n: ");
        int n = sc.nextInt();
        int square = n * n;
        int temp = square;
        int sum = 0;
        while (temp > 0) {
            int digit = temp % 10;
            sum = sum + digit;
            temp = temp / 10;
        }
        if (sum == n)
            System.out.println(n + " is a Neon number");
        else
            System.out.println(n + " is not a Neon number");
    }
}

Sample input: n = 9
Output: Enter n: 9 is a Neon number

How it works

  • First we compute square = n * n and copy it into temp, so the original square stays unchanged while we break it into digits.
  • Inside the while (temp > 0) loop, digit = temp % 10 picks the last digit of temp.
  • That digit is added to sum, and temp = temp / 10 drops the last digit. The loop ends when temp becomes 0.
  • For n = 9: square = 81, the digits 1 and 8 are added in turn, so sum = 9.
  • Finally, if (sum == n) we print that n is a Neon number, otherwise we print that it is not.

Dry run (trace table)

StepLineBoxsumnsquaretempdigitOutput
13Start
24sum = 00
36Read n09
47square = n * n0981
58temp = square098181
69temp > 0?098181
710digit = temp % 100981811
811sum = sum + digit1981811
912temp = temp / 10198181
109temp > 0?198181
1110digit = temp % 10198188
1211sum = sum + digit998188
1312temp = temp / 10998108
149temp > 0?998108
1514sum == n?998108
1615Print n is a Neon number9981089 is a Neon number
1718Stop998108

Time and space complexity

Time: O(log n) · Space: O(1). The loop runs once per digit of n², which is about 2·log10(n) iterations, using only a few variables.

Common mistakes

  • Adding the digits of n instead of the digits of n * n.
  • Looping on square directly, so square is destroyed and you cannot reuse it; use a copy like temp.
  • Forgetting to set sum = 0 before the loop, which gives a garbage total.

Viva questions

Q1. What is a Neon number, and can you give an example?

A Neon number is a number whose sum of the digits of its square equals the number itself. For example, 9: 9 squared is 81, and 8 + 1 = 9, so 9 is a Neon number.

Q2. Why is sum initialised to 0, and why do you copy square into temp?

sum starts at 0 because it is an accumulator, and any other starting value would add an extra amount to the answer. temp is a copy so the loop can destroy it by dividing by 10 while square stays unchanged.

Q3. Explain what lines 10 to 12 do in each iteration of the loop.

temp % 10 gives the last digit, which is added to sum. temp / 10 uses integer division to drop that last digit. This repeats until no digits are left.

Q4. What does the program print for n = 0 and for n = 1? Why?

For n = 0, the square is 0, so the loop never runs, sum stays 0, and 0 is reported as a Neon number. For n = 1, the square is 1 and the digit sum is 1, so 1 is also Neon.

Q5. What happens if the user enters a negative number such as -9?

The square n*n is positive (81), so its digit sum is 9. But 9 does not equal -9, so it prints not Neon. The program does not validate input, but the answer is still sensible.

Q6. Why does the loop condition use temp > 0, and what would go wrong with temp >= 0?

temp > 0 stops once all digits have been removed and temp becomes 0. With temp >= 0 the loop would never end, because 0 / 10 is always 0.

Q7. What is the time complexity of this program?

It is O(log n), or more precisely O(d), where d is the number of digits in n squared. Each iteration removes one digit, and the number of digits is proportional to log of the square.

Q8. What would change or break if n is very large, for example 100000?

n * n would overflow a 32-bit int, since 10^10 exceeds the limit of about 2.1 billion, giving a wrong square and a wrong result. To fix it, use long long for square and temp, and print with %lld where needed.

Frequently asked questions

What is a Neon number?

A Neon number is a number whose square has a digit sum equal to the number itself. For example, 9² = 81 and 8 + 1 = 9, so 9 is a Neon number.

Is 0 a Neon number?

Mathematically yes, since 0² = 0 and the digit sum is 0. In this program the loop does not run for 0, so sum stays 0 and it prints that 0 is a Neon number.

Which numbers are Neon numbers?

The well-known Neon numbers are 0, 1 and 9. For 1, 1² = 1 gives digit sum 1; for 9, 81 gives 9.

More programs

← All programs

Try AI Flowchart free →