Check Neon Number in C++

Check Neon Number in C++ (4 Programs).

IntermediateAdvanced Number ProgramsExample 11 of 13
check-neon-number.cpp
1#include <iostream>
2using namespace std;
3
4bool isNeon(int num) {
5 int square = num * num;
6 int sum = 0;
7
8 while (square > 0) {
9 sum += square % 10;
10 square /= 10;
11 }
12
13 return sum == num;
14}
15
16int main() {
17 int num;
18 cout << "Enter a number: ";
19 cin >> num;
20
21 if (isNeon(num)) {
22 cout << num << " is a Neon number" << endl;
23 } else {
24 cout << num << " is not a Neon number" << endl;
25 }
26
27 return 0;
28}

Output

Enter a number: 9
9 is a Neon number

What's going on

The program checks whether a given number is a Neon number. For example:

Input: 9
Calculation: 9² = 81, sum of digits of 81 = 8 + 1 = 9
Since 9 equals 9, it is a Neon number.

A Neon number must satisfy: sum of digits of (number²) = number

Example:

9 is a Neon number: 9² = 81, 8 + 1 = 9 ✓
1 is a Neon number: 1² = 1, 1 = 1 ✓
5 is NOT a Neon number: 5² = 25, 2 + 5 = 7 ≠ 5 ✗

Header File Used

This header provides:

cout for displaying output
cin for taking input from the user
1#include <iostream>

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

:

0: 0² = 0, sum of digits = 0, 0 = 0 ✓ (Neon)
1: 1² = 1, sum of digits = 1, 1 = 1 ✓ (Neon)
9: 9² = 81, sum of digits = 8 + 1 = 9, 9 = 9 ✓ (Neon)
5: 5² = 25, sum of digits = 2 + 5 = 7, 7 ≠ 5 ✗ (Not Neon)

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;

}

}

This function:

Takes a number as input.
Calculates its square.
Sums the digits of the square.
Returns true if the sum equals the original number.
1return sum == num;

Step-by-Step Algorithm

Let's trace through the algorithm for num = 9:

Step 1: Calculate Square

square = 9 × 9 = 81

Step 2: Extract and Sum Digits

Initialize: sum = 0, square = 81

Iteration 1:

remainder = 81 % 10 = 1 (extract rightmost digit)
sum = 0 + 1 = 1
square = 81 / 10 = 8 (remove rightmost digit)

Iteration 2:

remainder = 8 % 10 = 8
sum = 1 + 8 = 9
square = 8 / 10 = 0 (stop condition)

Step 3: Compare

sum = 9, num = 9
9 == 9 → return true

Result: 9 is a Neon number.

Understanding Digit Extraction

Modulo Operator (%)

:

square % 10 extracts the rightmost digit.
Example: 81 % 10 = 1, 8 % 10 = 8

Integer Division (/)

:

square / 10 removes the rightmost digit.
Example: 81 / 10 = 8, 8 / 10 = 0

Accumulation

:

sum += square % 10 adds each digit to the sum.
Continues until all digits are processed.

Main Function

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;

}

1int main() {

Process Flow

:

1.User enters a number (e.g., 9).
2.Call isNeon(9) to check.
3.Display result based on return value.

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';

}

Converts square to string.
Iterates through each character.
Converts each character to digit and sums.
1return sum == num;

Method 3: Using Recursion

int sumDigits(int n) {

if (n == 0) return 0;

}

bool isNeon(int num) {

return sumDigits(num * num) == num;

}

Recursive approach for summing digits.
Base case: when n becomes 0.
Recursive case: extract digit and recurse.
1return (n % 10) + sumDigits(n / 10);

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];

}

Stores digits in an array.
Sums array elements.
1return sum == num;

Displaying the Result

The program prints:

Output (for input 9):

9 is a Neon number

Or if not a Neon number:

5 is not a Neon number

1cout << num << " is a Neon number" << endl;

Why Only 0, 1, and 9?

Mathematical Proof

:

For a number n to be Neon: sum of digits of (n²) = n

Analysis

:

For n ≥ 10, n² has at least 3 digits.
Maximum sum of digits for a k-digit number is 9k.
For n = 10, n² = 100, max sum = 9, but 9 < 10.
This pattern continues for larger numbers.

Conclusion

:

Only single-digit numbers can be Neon numbers, and only 0, 1, and 9 satisfy the condition.

Common Use Cases

Number Theory

:

Studying special number properties.
Mathematical puzzles and problems.
Educational programming exercises.

Algorithm Practice

:

Digit manipulation techniques.
Understanding modulo and division operations.
Building accumulation patterns.

Interview Questions

:

Common coding interview problem.
Tests understanding of digit operations.
Foundation for more complex number problems.

Important Considerations

Edge Cases

:

0 is a Neon number (0² = 0, sum = 0).
1 is a Neon number (1² = 1, sum = 1).
Negative numbers: squares are positive, but original is negative, so never Neon.

Large Numbers

:

For very large numbers, square may cause integer overflow.
Consider using long long for larger numbers.
Or use string-based methods for very large numbers.

Efficiency

:

Time complexity: O(log(n²)) = O(log n) - logarithmic.
Space complexity: O(1) - constant space.

return 0;

This ends the program successfully.