In this article you will know How to Find an Armstrong Number Using Python. To make this Armstrong Number Program in Python, you need to have a basic understanding of Python.

Armstrong Number Program in Python

In this article you will know How to Find an Armstrong Number Using Python. To make this Armstrong Number Program in Python, you need to have a basic understanding of Python.

An Armstrong number, also known as a narcissistic number, is a number that is the sum of its own digits each raised to the power of the number of digits. 

In simpler terms, an n-digit number is an Armstrong number if the sum of its digits, each raised to the power of n, is equal to the number itself. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

Python Program to Check Armstrong Number

The process of finding Armstrong numbers in python involves breaking down a number into its individual digits, raising each digit to a power, summing these powered digits, and checking if the result equals the original number. Let’s break down the logic step by step:

  1. Get the Number of Digits: Find the number of digits in the given number. This can be achieved using the len() function after converting the number to a string.

  2. Extract Digits: Separate each digit from the number. This can be done by iterating through each digit in the string representation of the number.

  3. Raise Digits to the Power: Raise each digit to the power of the total number of digits.

  4. Sum the Powered Digits: Add up all the powered digits.

  5. Check for Equality: Compare the sum with the original number. If they are equal, the number is an Armstrong number.

Now, let’s translate this logic into a Python program.

Armstrong Number Program in Python

Below is the code for the Python Armstrong Number Program. This is a Simple Python Program to Check Armstrong Number. Below I have given all the code. But if you are a beginner then there is no reason to worry. Below I have given step-by-step explanation.

				
					def is_armstrong_number(number):
    # Step 1: Get the number of digits
    num_str = str(number)
    num_digits = len(num_str)

    # Step 2: Extract digits, 
    # Step 3: Raise digits to the power, 
    # Step 4: Sum the powered digits
    sum_of_powers = sum(int(digit) ** num_digits for digit in num_str)

    # Step 5: Check for equality
    return sum_of_powers == number

# Example Usage
number_to_check = 153
if is_armstrong_number(number_to_check):
    print(f"{number_to_check} is an Armstrong number.")
else:
    print(f"{number_to_check} is not an Armstrong number.")

				
			

Step by Step Explanation

If it is difficult to understand the above Armstrong Number Program in Python, then use the following explanations.

1. Function Definition:
				
					def is_armstrong_number(number):

				
			

This line defines a function named is_armstrong_number that takes a single parameter number. This function will be used to check whether the given number is an Armstrong number.

2. Get the Number of Digits:
				
					num_str = str(number)
num_digits = len(num_str)

				
			

The number is converted to a string (num_str), and then the length of the string (number of digits) is calculated and stored in the variable num_digits.

3. Extract Digits, Raise to the Power, and Sum:
				
					sum_of_powers = sum(int(digit) ** num_digits for digit in num_str)

				
			

This line uses a generator expression to iterate through each digit in num_str, convert it to an integer, raise it to the power of num_digits, and then sums up these powered digits. The result is stored in the variable sum_of_powers.

4. Check for Equality:
				
					return sum_of_powers == number

				
			

The function returns True if the sum_of_powers is equal to the original number, indicating that it is an Armstrong number. Otherwise, it returns False.

5. Example Usage:
				
					number_to_check = 153
if is_armstrong_number(number_to_check):
    print(f"{number_to_check} is an Armstrong number.")
else:
    print(f"{number_to_check} is not an Armstrong number.")

				
			

This part of the code sets number_to_check to 153 and then checks whether it is an Armstrong number using the is_armstrong_number function. The result is then printed accordingly.

In summary, the is_armstrong_number function encapsulates the logic to determine whether a given number is an Armstrong number or not. The example usage demonstrates how to use this function with a specific number.

Comment how you like this Python Program to Check Armstrong Number. Hopefully from this article you have learned How to Find an Armstrong Number Using Python.

An Armstrong number (also known as a narcissistic number, pluperfect digital invariant, or pluperfect number) is a number that is the sum of its own digits each raised to the power of the number of digits.

In this example, 153 is an Armstrong number because:

1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

So, the sum of the cubes of its digits is equal to the original number. If you run the above code, it will output: “153 is an Armstrong number.”

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

def is_armstrong_number(number):
# Convert the number to a string to find the number of digits
num_str = str(number)
num_digits = len(num_str)

# Calculate the sum of each digit raised to the power of the number of digits
armstrong_sum = sum(int(digit) ** num_digits for digit in num_str)

# Check if the sum is equal to the original number
return armstrong_sum == number

# Example usage:
num_to_check = int(input("Enter a number to check for Armstrongness: "))
if is_armstrong_number(num_to_check):
print(f"{num_to_check} is an Armstrong number.")
else:
print(f"{num_to_check} is not an Armstrong number.")

The formula for an Armstrong number (also known as a narcissistic number, pluperfect digital invariant, or pluperfect number) of n digits is:

a1n+a2n++akn=number

Where are the individual digits of the number, and is the number of digits in the number.

In other words, an n-digit number is an Armstrong number if the sum of its digits, each raised to the power of n, is equal to the original number.

For example, let’s take the Armstrong number 153 with 3 digits:

13+53+33=1+125+27=153

So, 153 is an Armstrong number because the sum of the cubes of its digits is equal to the original number.