Python Code Example: Neon number

The code checks if a given number is a “neon number” or not.

A neon number is a number that is equal to the sum of the digits of the square of the number.

Here’s the explanation of how the code works:

  1. The input number is stored in the variable num.
  2. The square of num is calculated and stored in the variable square.
  3. The while loop runs as long as square is not equal to 0.
  4. In each iteration of the loop, the last digit of square is extracted and stored in the variable digit.
  5. The sum of the extracted digits is calculated and stored in the variable sum.
  6. The square is updated by floor division of square by 10. This will remove the last digit of the square.
  7. After the while loop ends, the code checks if num is equal to sum.
  8. If num is equal to sum, then the code prints num is a neon number. If num is not equal to sum, then the code prints num is not a neon number.
sum = 0
print("Enter the number to check:")
num = int(input())
square = num * num

while (square != 0):
    digit = square % 10
    sum = sum + digit
    square = square // 10

if (num == sum):
    print(str(num) + " is a neon number.")
else:
    print(str(num) + " is not a neon number.")
Output
Enter a number to check for neon number:
9
9 is a neon number.

Enter a number to check for neon number:
30
30 is not a neon number.