您的位置:

Python 程序:检查一个数字是不是霓虹数字

编写一个 Python 程序来检查一个数字是不是霓虹数字,或者是否使用 while 循环。如果一个数等于平方数位数之和,它就是霓虹数。例如,9 是一个霓虹数字,因为 92 = 81,8 +1 = 9

在这个 python 例子中,首先,我们找到一个数的平方。接下来,把那个正方形分成几个独立的数字,求出总和。如果总和等于实际数字,它就是一个霓虹数字。

import math

Number = int(input("Enter the Number to Check Neon Number = "))
Sum = 0

squr = math.pow(Number, 2)
print("Square of a Given Digit = %d" %squr)

while squr > 0:
    rem = squr % 10
    Sum = Sum + rem
    squr = squr // 10

print("The Sum of the Digits   = %d" %Sum)

if Sum == Number:
    print("\n%d is a Neon Number." %Number)
else:
    print("%d is Not a Neon Number." %Number)

Python 程序检查一个数字是不是 neon 数或者不使用递归或者递归函数。

# Python Program to Check Neon Number
import math
Sum = 0

def neonNumber(squr):
    global Sum
    if squr > 0:
        rem = squr % 10
        Sum = Sum + rem
        neonNumber(squr // 10)
    return Sum

Number = int(input("Enter the Number to Check Neon Number = "))

squr = math.pow(Number, 2)
print("Square of a Given Digit = %d" %squr)

Sum = neonNumber(squr)
print("The Sum of the Digits   = %d" %Sum)

if Sum == Number:
    print("\n%d is a Neon Number." %Number)
else:
    print("%d is Not a Neon Number." %Number)
Enter the Number to Check Neon Number = 44
Square of a Given Digit = 1936
The Sum of the Digits   = 19
44 is Not a Neon Number.

Enter the Number to Check Neon Number = 9
Square of a Given Digit = 81
The Sum of the Digits   = 9

9 is a Neon Number.

使用 for 循环和 while 循环打印从 1 到 n 的霓虹数字的 Python 程序。

import math

MinNeon = int(input("Please Enter the Minimum Neon Value = "))
MaxNeon = int(input("Please Enter the Maximum Neon Value = "))

for i in range(MinNeon, MaxNeon + 1):
    Sum = 0
    squr = math.pow(i, 2)

    while squr > 0:
        rem = squr % 10
        Sum = Sum + rem
        squr = squr // 10

    if Sum == i:
        print(i, end = '  ')
Please Enter the Minimum Neon Value = 1
Please Enter the Maximum Neon Value = 10000
1  9