Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions exercises/1000_programs/medium/1134_armstrong.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
""" Check if number is Armstrong number
An Armstrong number is a number that equals the sum of its own digits
each raised to the power of the total number of digits. """

# we return the number of digits in n
def get_digits(n):
digits_count = 0

while n > 0:
digits_count += 1
n = n // 10

return digits_count

n = int(input("Enter a number: ")) # we assume that the user is going to give an int
total_of_digits = get_digits(n) # we get the total of digits in n
aux = n # we need to store the value to make the final comparation

sum = 0
while n > 0:
digit = n % 10 # get the last digit
sum += pow(digit, total_of_digits)
n //= 10

print(sum == aux)

Loading