Check Neon Number

Check Neon Number in C++ (4 Programs)

IntermediateTopic: Advanced Number Programs
Back

C++ Check Neon Number Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
using namespace std;

bool isNeon(int num) {
    int square = num * num;
    int sum = 0;
    
    while (square > 0) {
        sum += square % 10;
        square /= 10;
    }
    
    return sum == num;
}

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;
}
Output
Enter a number: 9
9 is a Neon number

Understanding Check Neon Number

This program teaches you how to check if a number is a Neon number in C++. A Neon number is a special number where the sum of the digits of its square equals the original number itself. This is an interesting number theory problem that helps students understand digit manipulation, squaring numbers, and accumulation patterns.

---

1. What This Program Does

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 ✗

---

2. Header File Used

This header provides:

cout for displaying output
cin for taking input from the user

---

#include <iostream>

3. 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).

---

4. 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.

---

    return sum == num;

5. 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.

---

6. 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.

---

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

}

int main() {

Process Flow

:

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

---

8. 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.
return 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.
    return (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.

---

return sum == num;

9. 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

---

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

10. 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.

---

11. 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.

---

12. 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.

---

13. return 0;

This ends the program successfully.

---

Summary

A Neon number satisfies: sum of digits of (number²) = number.
Only 0, 1, and 9 are Neon numbers in base-10.
Algorithm: square the number, sum its digits, compare with original.
Digit extraction uses modulo (%) and division (/) operations.
Understanding Neon numbers helps with digit manipulation and number theory.
Multiple methods exist: while loop, string, recursion, array.
Choose method based on needs: simplicity vs. learning vs. code organization.

This program is fundamental for beginners learning digit manipulation, understanding special number properties, and preparing for more complex number theory problems in C++ programs.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Check Neon Number

This C++ program is part of the "Advanced Number Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents