Python Program To Check Armstrong Number

What is Armstrong number?

Armstrong number in a given number base b is a number that is the sum of its own digits each raised to the power of the number of digits.

Example

153 = 13 + 53 + 33 = 1+125+27 = 153


Program



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# armstrong number in python
# take input from the user
num = int(input("Enter a number: "))

# find the length of num and this length is used in power
power = len(str(num))

# set count = 0 initially
count = 0

# now take a temprory num
temp = num

while temp > 0:
    # take a last digit from the number
    digit = temp % 10
    # add this number with the power in count
    count += digit ** power
    # now remove the last digit from temp(num)
    temp //= 10

# print result
if num == count:
    print(num,"is an Armstrong number")
else:
    print(num,"is not an Armstrong number")

# armstrong number in python - docodehere.com

Output


Enter a number: 153 153 is an Armstrong number

Next Post Previous Post
No Comment
Add Comment
comment url