How to Check if a Number is an Armstrong Number in Python 🐍


Check if a Number is an Armstrong Number in Python

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. It’s a great exercise for Python beginners to practice loops, conditionals, and number operations. In this blog, we’ll explore how to check if a number is an Armstrong number using Python.

By the end of this post, you’ll know how to:

  • Understand the concept of Armstrong numbers.
  • Write a Python program to check if a number is an Armstrong number.
  • Use loops and mathematical operations to solve the problem.

What is an Armstrong Number?

An Armstrong number is an integer such that the sum of its digits, each raised to the power of the number of digits in the number, is equal to the number itself.

For example:

  • ( 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 )
  • ( 9474 = 9^4 + 4^4 + 7^4 + 4^4 = 9474 )

Armstrong Number Formula

If a number ( n ) has ( d ) digits, then the number is an Armstrong number if:


How to Check if a Number is an Armstrong Number in Python

We’ll go through the steps to implement a Python program that checks whether a number is an Armstrong number.


Step-by-Step Guide

# Program to check if a number is an Armstrong number

# Step 1: Take user input
num = int(input("Enter a number: "))

# Step 2: Calculate the number of digits in the number
num_of_digits = len(str(num))

# Step 3: Initialize variables to store the sum of powered digits
sum_of_powers = 0
temp = num  # A temporary copy of the original number

# Step 4: Calculate the sum of each digit raised to the power of the number of digits
while temp > 0:
    digit = temp % 10  # Extract the last digit
    sum_of_powers += digit ** num_of_digits  # Raise the digit to the power of the number of digits and add to the sum
    temp //= 10  # Remove the last digit

# Step 5: Check if the sum of powers is equal to the original number
if sum_of_powers == num:
    print(f"{num} is an Armstrong number.")
else:
    print(f"{num} is not an Armstrong number.")

Explanation of the Code

  1. User Input:
  • We take the input number from the user using int(input()). The number is stored in the variable num.
  1. Find the Number of Digits:
  • To determine how many digits the number has, we convert the number to a string and calculate its length using len(str(num)).
  1. Initialize Variables:
  • sum_of_powers is initialized to 0. This variable will store the sum of each digit raised to the power of the number of digits.
  • temp is a temporary copy of the original number, which we will use to extract and manipulate digits.
  1. Calculate the Sum of Powered Digits:
  • We use a while loop to extract each digit from the number.
  • In each iteration of the loop, we:
    • Extract the last digit using temp % 10.
    • Raise the digit to the power of the number of digits (digit ** num_of_digits) and add it to sum_of_powers.
    • Remove the last digit from temp using integer division (temp //= 10).
  1. Check if the Number is Armstrong:
  • After calculating the sum of powered digits, we check if it’s equal to the original number. If they are equal, the number is an Armstrong number.

Sample Output

Enter a number: 153
153 is an Armstrong number.

In this example, the program checks if 153 is an Armstrong number. Since ( 153 = 1^3 + 5^3 + 3^3 ), the program outputs that 153 is an Armstrong number.


Test with a Non-Armstrong Number

Enter a number: 123
123 is not an Armstrong number.

For 123, the program calculates ( 1^3 + 2^3 + 3^3 = 36 ), which is not equal to 123, so the output confirms that 123 is not an Armstrong number.


Understanding Armstrong Numbers

Here are some important points about Armstrong numbers:

  • Armstrong numbers depend on the number of digits in the number. For example, a 3-digit Armstrong number follows the formula ( a^3 + b^3 + c^3 = abc ), whereas a 4-digit Armstrong number follows ( a^4 + b^4 + c^4 + d^4 = abcd ).
  • The smallest Armstrong numbers are:
  • 3-digit: 153, 370, 371, 407
  • 4-digit: 1634, 8208, 9474

Real-Life Applications of Armstrong Numbers

Although Armstrong numbers are mostly used in mathematics puzzles and educational exercises, they also help in developing problem-solving and logical thinking skills. Working with Armstrong numbers strengthens your understanding of loops, conditionals, and number manipulation in Python.


Conclusion

In this blog, we explored how to check if a number is an Armstrong number in Python. This exercise is a great way to practice basic programming concepts, including loops, conditionals, and mathematical operations. Whether you’re a beginner looking to improve your coding skills or just interested in mathematical puzzles, Armstrong numbers are a fun problem to tackle.


Would you like more beginner-friendly Python tutorials? Stay tuned for more coding challenges and solutions! 😊

Share with our team

Leave a Comment