EASY WAY TO PRINT AMSTRONG NUMBER OR NOT

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.")
Python

Explanation of the Code

  1. def is_armstrong(n):
    • A function named is_armstrong is defined that accepts a number n as an argument. This function will check whether the given number is an Armstrong number.
  2. digits = str(n)
    • The number n is converted into a string to allow easy iteration over its digits. For example, if the number is 153, the string "153" is created.
  3. 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.
  4. 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.
  5. 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.
  6. 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 to sum_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
  7. return sum_of_powers == n
    • After processing all the digits, the function checks if sum_of_powers is equal to the original number n. If they are equal, the function returns True indicating that the number is an Armstrong number, otherwise it returns False.
  8. 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, and int() converts it to an integer for further processing.
  9. if is_armstrong(number):
    • This line calls the is_armstrong() function and passes the user-entered number as an argument. If the function returns True, the program proceeds to the next step.
  10. 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.
  11. 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.
Python

Conclusion

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.

Leave a Reply

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

Resize text
Scroll to Top