We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 005ad34 + 3c516c3 commit 68d724aCopy full SHA for 68d724a
1 file changed
exercises/1000_programs/medium/1134_armstrong.py
@@ -0,0 +1,26 @@
1
+""" Check if number is Armstrong number
2
+An Armstrong number is a number that equals the sum of its own digits
3
+each raised to the power of the total number of digits. """
4
+
5
+# we return the number of digits in n
6
+def get_digits(n):
7
+ digits_count = 0
8
9
+ while n > 0:
10
+ digits_count += 1
11
+ n = n // 10
12
13
+ return digits_count
14
15
+n = int(input("Enter a number: ")) # we assume that the user is going to give an int
16
+total_of_digits = get_digits(n) # we get the total of digits in n
17
+aux = n # we need to store the value to make the final comparation
18
19
+sum = 0
20
+while n > 0:
21
+ digit = n % 10 # get the last digit
22
+ sum += pow(digit, total_of_digits)
23
+ n //= 10
24
25
+print(sum == aux)
26
0 commit comments