BUGSPOTTER

Armstrong Number In Python

What is an Armstrong Number ?

An Armstrong number (also called 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.

The concept is based on the number of digits in the number. For example, if the number has n digits, the sum of each digit raised to the power of n should be equal to the original number for it to be classified as an Armstrong number.

How it works ?

  1. Determine the number of digits (n) in the given number.
  2. For each digit in the number, raise it to the power of n (where n is the number of digits).
  3. Sum all the powered digits.
  4. If the sum is equal to the original number, then the number is an Armstrong number.

Example:

Let’s take the number 153:

  • Number of digits: 3 (since 153 has 3 digits)
  • Each digit raised to the power of 3: 
  • 1^3 + 5^3 + 3^3 = 1+125+27+=153
  • Since the sum is equal to the original number, 153 is an Armstrong number.

Steps to Check Armstrong Number for a Given Number:

  1. Convert the number to a string to easily access each digit.
  2. Count the number of digits in the number (this is n).
  3. Calculate the sum of each digit raised to the power n.
  4. Compare the sum with the original number. If they are equal, it is an Armstrong number.

Python Code Explaination

				
					def is_armstrong(num):
    # Step 1: Convert number to string to get each digit
    digits = str(num)  # This turns the number into a string, e.g., '153'
    
    # Step 2: Find the number of digits (n)
    n = len(digits)  # For 153, n will be 3
    
    # Step 3: Calculate the sum of digits raised to the power n
    sum_of_powers = sum(int(digit) ** n for digit in digits)
    
    # Step 4: Compare the sum to the original number
    return sum_of_powers == num  # If sum equals original, return True, else False

# Step 5: Ask the user for input
number = int(input("Enter a number: "))

# Step 6: Check if the number is an Armstrong number
if is_armstrong(number):
    print(f"{number} is an Armstrong number.")
else:
    print(f"{number} is not an Armstrong number.")

				
			

Detailed Explanation of the Code:

  1. str(num):

    • This converts the number into a string so we can access each digit individually.
    • For example, if num = 153, then digits = '153'.
  2. n = len(digits):

    • This calculates the number of digits in the number. In this case, since ‘153’ has 3 digits, n = 3.
    • This is important because we will raise each digit to the power of 3 (the number of digits).
  3. sum(int(digit) ** n for digit in digits):

    • This is a generator expression that iterates through each digit in digits.
    • For each digit, it converts the character to an integer (int(digit)), then raises it to the power of n.
    • It sums up all these powered values to give the total sum.
    • For 153:
      • 1^3 = 1
      • 5^3 = 125
      • 3^3 = 27
      • sum_of_powers = 1 + 125 + 27 = 153
  4. sum_of_powers == num:

    • Finally, we compare the sum of the powered digits with the original number. If they are equal, we return True, meaning it’s an Armstrong number.
    • If not, return False.

Let’s Go Through Some Examples:

Example 1: 153

  • n = 3 (because 153 has 3 digits)
  • Sum of each digit raised to the power of 3: 
  • 1^3 +5^3 + 3^3 = 1 + 125 +27 =153
  • Since the sum (153) is equal to the original number (153), it is an Armstrong number.

Example 2: 9474

  • n = 4 (because 9474 has 4 digits)
  • Sum of each digit raised to the power of 4: 
  • 9^4 + 4^4 + 7^4 = 6561 +256 + 2401 +256 =9474
  • Since the sum (9474) is equal to the original number (9474), it is an Armstrong number.

Example 3: 123

  • n = 3 (because 123 has 3 digits)
  • Sum of each digit raised to the power of 3: 
  • 1^3 + 2^3 +3^3 = 1+8+27= 36
  • Since the sum (36) is not equal to the original number (123), it is not an Armstrong number.

Edge Cases:

  • Single-digit numbers (1-9):
    • All single-digit numbers are Armstrong numbers because the number is equal to the sum of the number raised to the power of 1 (i.e., d1=dd^1 = d).
  • Large Numbers:
    • The program will still work for large numbers as Python handles large integers quite well.

Latest Posts

  • All Posts
  • Software Testing
  • Uncategorized
Load More

End of Content.

Categories

Enroll Now and get 5% Off On Course Fees