使用for循环而不是while循环在python 3中检查armstrong数字?

时间:2019-06-25 07:59:35

标签: python python-3.x

有什么办法使用for循环而不是while循环编写代码来检查python 3中的armstrong编号吗?

1 个答案:

答案 0 :(得分:-2)

# Python Program For Armstrong Number using For Loop

number = int(input("\nPlease Enter the Number to Check for Armstrong: "))

# Initializing Sum and Number of Digits
sum = 0
times = 0

# Calculating Number of individual digits
temp = number
while temp > 0:
    times = times + 1
    temp = temp // 10

# Finding Armstrong Number
temp = number
for n in range(1, temp + 1):
    reminder = temp % 10
    sum = sum + (reminder ** times)
    temp //= 10

if number == sum:
    print("\n %d is Armstrong Number.\n" %number)
else:
    print("\n %d is Not a Armstrong Number.\n" %number)