Neon Number in C++



A neon number is a number where the sum of digits of the square of the number is equal to the number. Let's take an example.

n = 9

square = 81

sum of digits of square = 8 + 1 = 9

So, the number 9 is a neon number.

We need to check whether the given number is a neon number or not. If the given number is a neon number, then print Yes else print No.

Algorithm

  • Initialise the number n.
  • Find the square of the number.
  • Find the sum of the digits of the square
  • If the sum of digits of the square is equal to the given number then result true else false.

Implementation

Following is the implementation of the above algorithm in C++

#include <bits/stdc++.h> using namespace std; int isNeonNumber(int x) {    int square = x * x;    int digitsSum = 0;    while (square != 0) {       digitsSum += (square % 10);       square = square / 10;    }    return digitsSum == x; } int main(void) {    string result;    result = isNeonNumber(1) ? "Yes" : "No";    cout << 1 << "->" << result << endl;    result = isNeonNumber(3) ? "Yes" : "No";       cout << 3 << "->" << result << endl;    result = isNeonNumber(9) ? "Yes" : "No";       cout << 9 << "->" << result << endl; }

Output

If you run the above code, then you will get the following result.

1->Yes 3->No 9->Yes
Updated on: 2021-10-23T08:01:01+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements