RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Python程序:检查一个数字是不是霓虹数字

创新互联python教程:

创新互联公司是一家专注网站建设、网络营销策划、成都微信小程序、电子商务建设、网络推广、移动互联开发、研究、服务为一体的技术型公司。公司成立10多年以来,已经为1000+成都阳光房各业的企业公司提供互联网服务。现在,服务的1000+客户与我们一路同行,见证我们的成长;未来,我们一起分享成功的喜悦。

编写一个 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 

分享标题:Python程序:检查一个数字是不是霓虹数字
本文网址:http://www.jxjierui.cn/article/cdsddoc.html