Neon Number in C++

In this tutorial, we will write a neon number program in Cpp. We will write two different programs.

  1. Check for neon number
  2. Display the neon number within the range

Neon Numbers

A number is said to be a neon number if the sum of the digits of a square of the number is equal t the original number.

Example: 9 is a Neon number (see explanation in the diagram).

Neon Number in C

Neon Number in C++

C++ Program to check for neon numbers.

//Cpp Program to check whether a given number is a Neon number or not
#include <iostream>
using namespace std;

int main()
{
  int num;

  cout << "Enter the number: ";
  cin >> num;

  int square = num * num;
  int sum = 0;

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

  if (sum == num)
    cout << num << " is a Neon number";
  else
    cout << num << " is NOT a Neon number";

  return 0;
}

Output:

Enter the number: 9
9 is a Neon number


C++ program to display all the neon number within a given range

//Cpp Program to check whether a given number is a Neon number or not
#include <iostream>
using namespace std;

int checkForNeon(int x)
{
  int square = x * x;
  int sumOfDigits = 0;

  while (square != 0)
  {
    sumOfDigits = sumOfDigits + square % 10;
    square /= 10;
  }

  return (sumOfDigits == x);
}

// Main function
int main(void)
{
  int lowestNum, highestNum;

  cout << "ENTER THE STARTING AND FINAL NUMBER FOR THE RANGE TO CHECK\n";

  cout << "\nEnter the lowest number: ";
  cin >> lowestNum;

  cout << "Enter the Highest number: ";
  cin >> highestNum;

  cout << "\nNeon Number between them are:";
  for (int i = lowestNum; i <= highestNum; i++)
  {
    if (checkForNeon(i))
      cout << i << " ";
  }
}

Output:

ENTER THE STARTING AND FINAL NUMBER FOR THE RANGE TO CHECK

Enter the lowest number: 1
Enter the Highest number: 2000

Neon Number between them are:1 9