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
- Step 1: Start.
- Step 2: Set sum = 0.
- Step 3: Read n.
- Step 4: Set square = n * n.
- Step 5: Set temp = square.
- Step 6: If temp > 0, go to step 7; otherwise go to step 10.
- Step 7: Set digit = temp % 10.
- Step 8: Set sum = sum + digit.
- Step 9: Set temp = temp / 10. Go to step 6.
- Step 10: If sum == n, go to step 11; otherwise go to step 12.
- Step 11: Print n is a Neon number. Go to step 13.
- Step 12: Print n is not a Neon number.
- 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)
| Step | Line | Box | sum | n | square | temp | digit | Output |
|---|---|---|---|---|---|---|---|---|
| 1 | 3 | Start | ||||||
| 2 | 4 | sum = 0 | 0 | |||||
| 3 | 6 | Read n | 0 | 9 | ||||
| 4 | 7 | square = n * n | 0 | 9 | 81 | |||
| 5 | 8 | temp = square | 0 | 9 | 81 | 81 | ||
| 6 | 9 | temp > 0? | 0 | 9 | 81 | 81 | ||
| 7 | 10 | digit = temp % 10 | 0 | 9 | 81 | 81 | 1 | |
| 8 | 11 | sum = sum + digit | 1 | 9 | 81 | 81 | 1 | |
| 9 | 12 | temp = temp / 10 | 1 | 9 | 81 | 8 | 1 | |
| 10 | 9 | temp > 0? | 1 | 9 | 81 | 8 | 1 | |
| 11 | 10 | digit = temp % 10 | 1 | 9 | 81 | 8 | 8 | |
| 12 | 11 | sum = sum + digit | 9 | 9 | 81 | 8 | 8 | |
| 13 | 12 | temp = temp / 10 | 9 | 9 | 81 | 0 | 8 | |
| 14 | 9 | temp > 0? | 9 | 9 | 81 | 0 | 8 | |
| 15 | 14 | sum == n? | 9 | 9 | 81 | 0 | 8 | |
| 16 | 15 | Print n is a Neon number | 9 | 9 | 81 | 0 | 8 | 9 is a Neon number |
| 17 | 18 | Stop | 9 | 9 | 81 | 0 | 8 |
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.