Introduction
An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits, each raised to the power of the number of digits. For example, the number 153
is an Armstrong number because:153=13+53+33=1+125+27=153153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153153=13+53+33=1+125+27=153
In this Python program, we will check if a given number is an Armstrong number. Below, you’ll find the explanation of each line of code along with the logic behind the program.
Python Program to Check for Armstrong Number
# Function to check if a number is an Armstrong number
def is_armstrong(n):
# Convert the number to a string to find the number of digits
digits = str(n)
# Calculate the number of digits in the number
num_digits = len(digits)
# Initialize a variable to hold the sum of the digits raised to the power
sum_of_powers = 0
# Loop through each digit in the number
for digit in digits:
# Convert the digit back to an integer and raise it to the power of num_digits
sum_of_powers += int(digit) ** num_digits
# Check if the sum of the powered digits equals the original number
return sum_of_powers == n
# Input: Get the number from the user
number = int(input("Enter a number: "))
# Check if the number is an Armstrong number and print the result
if is_armstrong(number):
print(f"{number} is an Armstrong number.")
else:
print(f"{number} is not an Armstrong number.")
PythonExplanation of the Code
def is_armstrong(n):
- A function named
is_armstrong
is defined that accepts a numbern
as an argument. This function will check whether the given number is an Armstrong number.
- A function named
digits = str(n)
- The number
n
is converted into a string to allow easy iteration over its digits. For example, if the number is153
, the string"153"
is created.
- The number
num_digits = len(digits)
- The variable
num_digits
stores the number of digits in the number. This is done by calculating the length of the string representation of the number. For instance,153
has 3 digits.
- The variable
sum_of_powers = 0
- A variable
sum_of_powers
is initialized to store the sum of each digit raised to the power of the total number of digits. This will accumulate the sum as the digits are processed.
- A variable
for digit in digits:
- A
for
loop is used to iterate over each digit in the number. Since the number was converted to a string earlier, each character in that string represents a digit.
- A
sum_of_powers += int(digit) ** num_digits
- For each digit, it is first converted back to an integer (since it was a string character) and then raised to the power of
num_digits
. The result is added tosum_of_powers
. - For example, for the number
153
, the calculation will be:- 13=11^3 = 113=1
- 53=1255^3 = 12553=125
- 33=273^3 = 2733=27
- 1+125+27=1531 + 125 + 27 = 1531+125+27=153
- For each digit, it is first converted back to an integer (since it was a string character) and then raised to the power of
return sum_of_powers == n
- After processing all the digits, the function checks if
sum_of_powers
is equal to the original numbern
. If they are equal, the function returnsTrue
indicating that the number is an Armstrong number, otherwise it returnsFalse
.
- After processing all the digits, the function checks if
number = int(input("Enter a number: "))
- This line takes input from the user. The
input()
function gets the number as a string from the user, andint()
converts it to an integer for further processing.
- This line takes input from the user. The
if is_armstrong(number):
- This line calls the
is_armstrong()
function and passes the user-entered number as an argument. If the function returnsTrue
, the program proceeds to the next step.
- This line calls the
print(f"{number} is an Armstrong number.")
- If the number is an Armstrong number, this statement prints a message indicating that the number is indeed an Armstrong number.
print(f"{number} is not an Armstrong number.")
- If the number is not an Armstrong number, this statement prints a message indicating that the number is not an Armstrong number.
Example Output
Enter a number: 153
153 is an Armstrong number.
Enter a number: 123
123 is not an Armstrong number.
PythonConclusion
This Python program efficiently checks whether a given number is an Armstrong number. By breaking the problem into smaller steps such as iterating over the digits and raising them to a power, this approach makes it easier to understand the concept of Armstrong numbers. This solution also demonstrates the importance of using basic programming constructs like loops and conditional statements to solve mathematical problems.