-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion 34.py
More file actions
33 lines (26 loc) · 1.19 KB
/
Question 34.py
File metadata and controls
33 lines (26 loc) · 1.19 KB
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
# Concept name : Number Problem
# Question : Armstrong Numbers
# You are given a 3-digit number n, Find whether it is an Armstrong number or not.
# An Armstrong number of three digits is a number such that the sum of the cubes of its digits is equal to the number itself.
# 371 is an Armstrong number since 33 + 73 + 13 = 371.
# Examples:
# Input: n = 153
# Output: true
# Explanation: 153 is an Armstrong number since 13 + 53 + 33 = 153.
# Solution :
class Solution:
def armstrongNumber (self, n):
temp=n
sum_arm=0
while temp>0:
digit=temp%10
sum_arm+=digit**3
temp=temp//10
if n==sum_arm:
return True
return False
# Explanation: Armstrong number of 3 digits → sum of cubes of its digits equals the number itself.
# 1. Extract each digit using modulo and integer division.
# 2. Cube each digit and add to sum.
# 3. If the sum equals the original number → it's an Armstrong number.
# 4. Return 1 if True, else 0.