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.
# 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.")
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.
sum = 0
print("Enter the number to check:")
num = int(input())
square = num * num
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.print("Enter the number to check:")
prints a message asking the user to enter a number.num = int(input())
reads the user input, converts it to an integer, and assigns it to the variable num
.square = num * num
calculates the square of num
and assigns the result to the variable square
.while (square != 0):
digit = square % 10
sum = sum + digit
square = square // 10
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
.digit = square % 10
extracts the last digit of square
by taking the remainder of square
divided by 10. This digit is stored in digit
.sum = sum + digit
adds the extracted digit digit
to the variable sum
.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.if (num == sum):
print(str(num) + " is a neon number.")
else:
print(str(num) + " is not a neon number.")
if (num == sum):
checks if the original number num
is equal to the sum of the digits of its square.num
is equal to sum
, the code executes print(str(num) + " is a neon number.")
, printing that num
is a neon number.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.