What’s here -:

Step 1: Understanding the Problem
An Armstrong number is a number that is equal to the sum of its individual digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.
Step 2: Developing the Algorithm
To determine if a number is an Armstrong number, we need to follow these steps:
1. Accept the number as input.
2. Convert the number to a string to count the number of digits.
3. Initialize a variable sum to 0.
4. Iterate over each digit in the number.
5. Raise each digit to the power of the total number of digits and add it to sum.
6. Compare sum with the original number.
7. If they are equal, the number is an Armstrong number. Otherwise, it is not.
Step 3: Writing the Python Code
Here’s the Python code to check if a number is armstrong:
def is_armstrong_number(number):
num_str = str(number)
num_digits = len(num_str)
armstrong_sum = 0
for digit in num_str:
armstrong_sum += int(digit) ** num_digits
return armstrong_sum == number
# Test the function
number = 153
if is_armstrong_number(number):
print(number, "is an Armstrong number.")
else:
print(number, "is not an Armstrong number.")
O/P
153 is an Armstrong number.
Time Complexity: O(len(n))
Step 4: Code Explanation
1. We define a function is_armstrong_number that takes the number as an argument and checks if it is an Armstrong number.
2. We convert the number to a string and store it in num_str to count the number of digits.
3. The variable num_digits stores the length of num_str, representing the total number of digits.
4. We initialize armstrong_sum to 0 to hold the sum of the digits raised to the power of num_digits.
5. Using a for loop, we iterate over each digit in num_str.
6. We convert each digit back to an integer using int(digit) and raise it to the power of num_digits using the exponentiation operator **.
7. The resulting value is added to armstrong_sum.
8.Finally, we compare armstrong_sum with the original number. If they are equal, the function returns True, indicating that the number is an Armstrong number. Otherwise, it returns False.
Step 5: Dry Run the Code
Let’s do a dry run of the code iteration-wise for number = 153:
Iteration 1:
digit is ‘1’.
armstrong_sum is 1^3 = 1.
Iteration 2:
digit is ‘5’.
armstrong_sum is 1^3 + 5^3 = 1 + 125 = 126.
Iteration 3:
digit is ‘3’.
armstrong_sum is 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.
The loop ends, and we compare armstrong_sum with the original number. Since they are equal, the code will print “153 is an Armstrong number.”
Feel free to modify and experiment with the code to understand it better and explore other variations or optimizations.
