C Program to Find Neon Number with Output and Explanation
A neon number is a number where the sum of digits of its square equals the number itself; in C, you can find it by squaring the number, summing the digits of the square, and comparing it to the original number using code like
if (sum == num).Examples
Input9
Output9 is a neon number
Input5
Output5 is not a neon number
Input1
Output1 is a neon number
How to Think About It
To find if a number is neon, first square the number. Then, add all digits of this squared value. If the sum equals the original number, it is a neon number; otherwise, it is not.
Algorithm
1
Get input number from the user2
Calculate the square of the number3
Initialize sum to zero4
Extract each digit of the square and add to sum5
Compare sum with the original number6
Print if the number is neon or notCode
c
#include <stdio.h> int main() { int num, square, sum = 0, temp; printf("Enter a number: "); scanf("%d", &num); square = num * num; temp = square; while (temp > 0) { sum += temp % 10; temp /= 10; } if (sum == num) printf("%d is a neon number\n", num); else printf("%d is not a neon number\n", num); return 0; }
Output
Enter a number: 9
9 is a neon number
Dry Run
Let's trace the input 9 through the code
1
Input number
num = 9
2
Calculate square
square = 9 * 9 = 81
3
Sum digits of square
sum = 8 + 1 = 9
4
Compare sum with original
sum (9) == num (9) → true
5
Print result
"9 is a neon number"
| Iteration | temp | sum |
|---|---|---|
| 1 | 81 | 0 |
| 2 | 8 | 1 |
| 3 | 0 | 9 |
Why This Works
Step 1: Square the number
We calculate square = num * num to get the number to analyze.
Step 2: Sum digits of the square
We extract each digit of square using modulus and division, adding them to sum.
Step 3: Compare sum with original number
If sum == num, the number is neon; otherwise, it is not.
Alternative Approaches
Using a function to sum digits
c
#include <stdio.h> int sumDigits(int n) { int s = 0; while (n > 0) { s += n % 10; n /= 10; } return s; } int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (sumDigits(num * num) == num) printf("%d is a neon number\n", num); else printf("%d is not a neon number\n", num); return 0; }
This approach improves readability by separating digit summation into a reusable function.
Using recursion to sum digits
c
#include <stdio.h> int sumDigits(int n) { if (n == 0) return 0; return (n % 10) + sumDigits(n / 10); } int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (sumDigits(num * num) == num) printf("%d is a neon number\n", num); else printf("%d is not a neon number\n", num); return 0; }
This recursive method is elegant but may be less efficient for very large numbers.
Complexity: O(log n) time, O(1) space
Time Complexity
The time depends on the number of digits in the square, which is proportional to log n. Summing digits requires iterating over each digit.
Space Complexity
Only a few integer variables are used, so space complexity is constant O(1).
Which Approach is Fastest?
The iterative approach is fastest and simplest; recursive methods add overhead but improve readability.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative digit sum | O(log n) | O(1) | Simple and efficient |
| Function abstraction | O(log n) | O(1) | Readable and reusable code |
| Recursive digit sum | O(log n) | O(log n) | Elegant but uses call stack |
Always check the sum of digits of the square, not the number itself, to identify a neon number.
Beginners often forget to sum the digits of the square and instead sum digits of the original number.