Python Program to check Armstrong Number

# Function to check whether a number is an Armstrong number or not
def armstrong_number(n):
    # Convert the number to a string to count the number of digits
    num_str = str(n)
    num_digits = len(num_str)
    
    # Calculate the sum of the cubes of each digit
    sum = 0
    for digit in num_str:
        sum += int(digit) ** num_digits
    
    # Check whether the sum is equal to the original number
    if sum == n:
        return True
    else:
        return False

# Example usage
n = 153
if armstrong_number(n):
    print(n, "is an Armstrong number")
else:
    print(n, "is not an Armstrong number")

An Armstrong number is a number that is equal to the sum of its 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.

To check whether a given number is an Armstrong number, we can use a simple algorithm. First, we convert the number to a string to count the number of digits. Then we calculate the sum of the cubes of each digit using a loop. Finally, we compare the sum with the original number. If they are equal, the number is an Armstrong number, otherwise it’s not.

Leave a comment