編寫一個 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
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/237332.html