#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
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
#include <iostream>
This header provides:
- cout for displaying output
- cin for taking input from the user
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;
}
return sum == num;
}
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.
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 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:
- User enters a number (e.g., 9).
- Call isNeon(9) to check.
- 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'; } return sum == num;
- Converts square to string.
- Iterates through each character.
- Converts each character to digit and sums.
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; }
- Recursive approach for summing digits.
- Base case: when n becomes 0.
- Recursive case: extract digit and recurse.
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;
- Stores digits in an array.
- Sums array elements.
9. 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
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.