Python Program Check Armstrong Number

By | October 31, 2023

Python Program Check Armstrong Number – An Armstrong number (also known as a narcissistic number or a pluperfect digital invariant) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. In the case of a 3-digit number, for example, the number must equal the sum of the cubes of its digits.

Python Program Check Armstrong Number

Here’s a Python program to check if a given number is an Armstrong number:

Definition: Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.

Source Code

def is_armstrong_number(number):
    num_str = str(number)
    num_digits = len(num_str)
    total = 0

    for digit_char in num_str:
        digit = int(digit_char)
        total += digit ** num_digits

    return total == number

# Input from the user
num = int(input("Enter a number to check if it's an Armstrong number: "))

if is_armstrong_number(num):
    print(num, "is an Armstrong number.")
else:
    print(num, "is not an Armstrong number.")

Output

Enter a number to check if it’s an Armstrong number: 535
535 is not an Armstrong number.
>>>
===================== RESTART: F:/Python37/armstrong.py =====================
Enter a number to check if it’s an Armstrong number: 153
153 is an Armstrong number.
>>>

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *