Python Program to Check Armstrong Number

Introduction

In this post, I am going to write a python program to check armstrong number. We will read a number from the user and will check whether that number is armstrong or not.

Also Read: Python Program to Check If Two Strings are Anagram

What is Armstrong Number?

An Armstrong number is a number such that the sum of its digits when raised to the power of the number of digits is equal to the number itself. For example, 371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371

Suppose the input number is 153. Now, if we add cube of each digits and if we get the same number then we can say that this number is armstrong number. For example, 13 + 53 + 33 = 1 + 125 + 27 = 153. Therefore, we can 153 is armstrong number.

Take another number i.e. 123. Now, the addition of cube of each digits of 123 should be 123, otherwise that number will not be armstrong number. So the 13 + 23 + 33 = 1 + 8 + 27 = 36 which is not 123. Therefore, we can say 123 is not an armstrong number.

Steps to Check Armstrong Number

To check if a number is an Armstrong number in Python, we can use a simple function that follows these steps:

  1. Convert the number to a string and store it in a variable. This will allow us to easily access each digit of the number.
  2. Initialize a variable to store the sum of the digits raised to the power of the number of digits.
  3. Iterate over each digit in the number and raise it to the power of the number of digits. Add the result to the sum variable.
  4. If the sum is equal to the number, return True. Otherwise, return False.

Here’s what the function might look like:

def is_armstrong(num):
    num_str = str(num)
    sum = 0
    for digit in num_str:
        sum += int(digit) ** len(num_str)
    return sum == num

To test this function, we can write some test cases:

assert is_armstrong(371) == True
assert is_armstrong(407) == True
assert is_armstrong(1634) == True
assert is_armstrong(153) == True
assert is_armstrong(9474) == True
assert is_armstrong(9474) == True
assert is_armstrong(54748) == False
assert is_armstrong(4) == False

All of these test cases should return True, indicating that the function is working correctly.

That’s it! With just a few lines of code, we were able to write a Python program to check if a number is an Armstrong number. I hope this was helpful!

Here is a more detailed explanation of the Python function to check if a number is an Armstrong number:

First, we start by converting the number to a string and storing it in a variable called num_str. This is necessary because we need to access each individual digit of the number, and the easiest way to do this is to convert the number to a string.

Next, we initialize a variable called sum to store the sum of the digits raised to the power of the number of digits. This is the key to determining if a number is an Armstrong number, as it must equal the original number.

Then, we use a for loop to iterate over each digit in num_str. For each digit, we raise it to the power of the number of digits (which we can get by using the len() function) and add the result to the sum variable.

Finally, we check if sum is equal to the original number. If it is, we return True. Otherwise, we return False.

Here is the complete code again, with some additional comments to explain each part:

Python Program to Check Armstrong Number

def is_armstrong(num):
    # Convert the number to a string so we can access each digit
    num_str = str(num)
    
    # Initialize a variable to store the sum of the digits raised to the power of the number of digits
    sum = 0
    
    # Iterate over each digit in the number
    for digit in num_str:
        # Raise the digit to the power of the number of digits and add it to the sum
        sum += int(digit) ** len(num_str)
    
    # Return True if the sum is equal to the original number, False otherwise
    return sum == num

I hope this helps to clarify how the function works! Let me know if you have any questions.

Also Read: Python Program to Delete an Element From a Dictionary

Thank you.

Leave a Comment