Skip to content

Commit 4807bea

Browse files
Update Armstrong Number.py
1 parent 7b022e8 commit 4807bea

1 file changed

Lines changed: 34 additions & 12 deletions

File tree

Armstrong Number.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
num = int(input("Enter a number: "))
22

3-
# initialize sum
4-
sum = 0
3+
# # initialize sum
4+
# sum = 0
55

6-
# find the sum of the cube of each digit
7-
temp = num
8-
while temp > 0:
9-
digit = temp % 10
10-
sum += digit ** 3
11-
temp //= 10
6+
# # find the sum of the cube of each digit
7+
# temp = num
8+
# while temp > 0:
9+
# digit = temp % 10
10+
# sum += digit ** 3
11+
# temp //= 10
1212

13-
# display the result
14-
if num == sum:
15-
print(num,"is an Armstrong number")
13+
# # display the result
14+
# if num == sum:
15+
# print(num,"is an Armstrong number")
16+
# else:
17+
# print(num,"is not an Armstrong number")
18+
19+
20+
# one-liner armstrong check
21+
is_armstrong = lambda num: sum([int(i)**3 for i in str(num)]) == num
22+
23+
# some detailed code
24+
def isArmstrong(num):
25+
digits = tuple(str(num)) # create a tuple of digits
26+
digit_cube = [] # create a list to store cube of digits
27+
for digit in digits: # loop over every digit
28+
digit_cube.append(int(digit)**3) # add the cube of each digit to the cube list
29+
sum_of_cubes = sum(digit_cube) # calculate the sum of cubes
30+
if sum_of_cubes == num: # check if sum is equal to the original number
31+
return True
32+
else:
33+
return False
34+
35+
# driver code
36+
if is_armstrong(num) and isArmstrong(num):
37+
print(num, "is an Armstrong number")
1638
else:
17-
print(num,"is not an Armstrong number")
39+
print(num, "is not an Armstrong number")

0 commit comments

Comments
 (0)