Check Neon Number in C++
Check whether a number is a neon number in C++: square it, add the digits of the square, and compare. Traces 9 (81 → 8 + 1 = 9) and covers overflow of the square.
1#include <iostream>2using namespace std;34bool isNeon(int num) {5 int square = num * num;6 int sum = 0;78 while (square > 0) {9 sum += square % 10;10 square /= 10;11 }1213 return sum == num;14}1516int main() {17 int num;18 cout << "Enter a number: ";19 cin >> num;2021 if (isNeon(num)) {22 cout << num << " is a Neon number" << endl;23 } else {24 cout << num << " is not a Neon number" << endl;25 }2627 return 0;28}
Sample Output
9 is a Neon number
81 → 8 + 1 = 9.
In this one
- What a neon number is
- How to sum the digits of a number
- How a bool helper keeps main simple
- When num * num overflows int
How it works
- 1
Read 9.
- 2
square = 81.
- 3
Sum digits: 1 + 8 = 9.
- 4
9 == 9, so it is neon.
Edge cases
Input 0
Enter 0.
square is 0, the loop does not run, sum is 0, so 0 is reported as neon. By the definition 0² = 0 and digit sum 0, so that is correct.
Large input
Enter 50000.
50000² = 2.5 billion overflows int, which is undefined behavior. Use long long for the square.
What's going on
The program checks whether a given number is a Neon number. For example:
A Neon number must satisfy: sum of digits of (number²) = number
Example:
Header File Used
#include <iostream>
This header provides:
Understanding Neon Numbers
Definition:
A Neon number is a number where the sum of digits of its square equals the number itself.
Mathematical Expression:
If n is a Neon number, then: sum of digits of (n²) = n
Examples:
Known Neon Numbers:
Only 0, 1, and 9 are Neon numbers in base-10 (decimal system).
Function: isNeon()
bool isNeon(int num) {
int square = num * num;
int sum = 0;
while (square > 0) {
sum += square % 10;
square /= 10;
}
return sum == num;
}
This function:
Step-by-Step Algorithm
Let's trace through the algorithm for num = 9:
Step 1: Calculate Square
Step 2: Extract and Sum Digits
Iteration 1:
Iteration 2:
Step 3: Compare
Result: 9 is a Neon number.
Understanding Digit Extraction
Modulo Operator (%):
Integer Division (/):
Accumulation:
Main Function
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isNeon(num)) {
cout << num << " is a Neon number" << endl;
} else {
cout << num << " is not a Neon number" << endl;
}
return 0;
}
Process Flow:
Other Methods (Mentioned but not shown in code)
Method 2: Using String Conversion
int square = num * num;
string squareStr = to_string(square);
int sum = 0;
for (char c : squareStr) {
sum += c - '0';
}
return sum == num;
Method 3: Using Recursion
int sumDigits(int n) {
if (n == 0) return 0;
return (n % 10) + sumDigits(n / 10);
}
bool isNeon(int num) {
return sumDigits(num * num) == num;
}
Method 4: Using Array
int square = num * num;
int digits[10], count = 0;
while (square > 0) {
digits[count++] = square % 10;
square /= 10;
}
int sum = 0;
for (int i = 0; i < count; i++) {
sum += digits[i];
}
return sum == num;
Displaying the Result
The program prints:
cout << num << " is a Neon number" << endl;
Output (for input 9):
9 is a Neon number
Or if not a Neon number:
5 is not a Neon number
Why Only 0, 1, and 9?
Mathematical Proof:
For a number n to be Neon: sum of digits of (n²) = n
Analysis:
Conclusion:
Only single-digit numbers can be Neon numbers, and only 0, 1, and 9 satisfy the condition.
Common Use Cases
Number Theory:
Algorithm Practice:
Interview Questions:
Important Considerations
Edge Cases:
Large Numbers:
Efficiency:
return 0;
This ends the program successfully.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Is 5 a neon number?
Pick an answer to check it.
Question 2
Digit sum of 144?
Pick an answer to check it.
Questions people ask
Which numbers are neon?
Only 0, 1, and 9.
Why use a function?
It separates the test from input and output.
Does this work for negatives?
No. The square is positive but sum compares with a negative num, so negatives are never neon.