Data types and variables
Operators
Modules and Packages
Conversion Programs
More Code Examples
Cheat Sheet

Python Code Example: Neon Number

This Python code checks if a given number is a neon number. A neon number is a number for which the sum of the digits of its square equals the original number. The program prompts the user to input a number, calculates its square, and then sums the digits of the square. Finally, it compares this sum to the original number and prints whether the number is a neon number or not.

Code Example

# Initialize the sum variable to 0
sum = 0

# Prompt the user to enter a number
print("Enter the number to check:")

# Read the input and convert it to an integer
num = int(input())

# Calculate the square of the number
square = num * num

# Loop to sum the digits of the square
while (square != 0):
    # Extract the last digit of the square
    digit = square % 10
    
    # Add the digit to the sum
    sum = sum + digit
    
    # Remove the last digit from the square
    square = square // 10

# Check if the original number is equal to the sum of the digits of its square
if (num == sum):
    # If true, print that the number is a neon number
    print(str(num) + " is a neon number.")
else:
    # If false, print that the number is not a neon number
    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.

Code Explanation

Initialize Variables and Input

sum = 0
print("Enter the number to check:")
num = int(input())
square = num * num
  1. Initialize sum: sum = 0 initializes a variable sum to 0. This variable will be used to store the sum of the digits of the square of the number.
  2. Prompt for Input: print("Enter the number to check:") prints a message asking the user to enter a number.
  3. Read Input: num = int(input()) reads the user input, converts it to an integer, and assigns it to the variable num.
  4. Calculate Square: square = num * num calculates the square of num and assigns the result to the variable square.

Sum the Digits of the Square

while (square != 0):
    digit = square % 10
    sum = sum + digit
    square = square // 10
  1. While Loop: while (square != 0): initiates a loop that continues as long as square is not equal to 0. This loop extracts and sums the digits of square.
  2. Extract Last Digit: digit = square % 10 extracts the last digit of square by taking the remainder of square divided by 10. This digit is stored in digit.
  3. Add Digit to Sum: sum = sum + digit adds the extracted digit digit to the variable sum.
  4. Remove Last Digit: square = square // 10 removes the last digit from square by performing integer division by 10. This effectively shifts all digits of square one place to the right.

Check for Neon Number

if (num == sum):
    print(str(num) + " is a neon number.")
else:
    print(str(num) + " is not a neon number.")
  1. Comparison: if (num == sum): checks if the original number num is equal to the sum of the digits of its square.
  2. Print Result:
    • If num is equal to sum, the code executes print(str(num) + " is a neon number."), printing that num is a neon number.
    • If num is not equal to sum, the code executes print(str(num) + " is not a neon number."), printing that num is not a neon number.