-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathArmstrong.py
56 lines (35 loc) · 914 Bytes
/
Armstrong.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Python program to check if the number is an Armstrong number or not
# take input from the user
num = int(input("Enter a number: "))
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit**3
temp //= 10
# display the result
if num == sum:
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
# Check Armstrong number of n digits
# 2nd method
num = 1634 # you can change the num value.
# Changed num variable to string,
# and calculated the length (number of digits)
order = len(str(num))
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit**order
temp //= 10
# display the result
if num == sum:
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")