C++ Program To Check And Print Neon Number in a Given Range

Last Updated : 18 Aug, 2026

A Neon number is a number whose square has a digit sum equal to the original number. For example, 9 is a Neon number because 9² = 81 and 8 + 1 = 9.

  • The square of the number is calculated first.
  • The digits of the square are added and compared with the original number.

Illustration

Input: 9
Output: Neon Number

Explanation: 9² = 81 and 8 + 1 = 9, so 9 is a Neon number.

Input: 12
Output: Not a Neon Number

Explanation: 12² = 144 and 1 + 4 + 4 = 9, which is not equal to 12.

Approach

To find Neon numbers in a given range:

  • Traverse each number in the given range.
  • Calculate its square.
  • Find the sum of the digits of the square.
  • Compare the digit sum with the original number.
  • Print the number if both values are equal.
C++
#include <iostream>
using namespace std;

bool isNeon(int n)
{
    int square = n * n;
    int sum = 0;

    while (square > 0) {
        sum += square % 10;
        square /= 10;
    }

    return sum == n;
}

int main()
{
    int n = 10000;

    for (int i = 1; i <= n; i++) {
        if (isNeon(i))
            cout << i << " ";
    }

    return 0;
} 

Output
1 9 

Explanation: For each number, the program first calculates its square and then extracts its digits using the % operator. The digits are added together and the resulting sum is compared with the original number. If they are equal, the number is printed as a Neon number.

Comment