Armstrong Number in Python

An armstrong number in a given number base b is a number that is the sum of its own digits each raised to the power of the number of digits

armstrong in python

What is an Armstrong Number?

An Armstrong number (or narcissistic number) in a given number base is a number that equals the sum of its own digits, each raised to the power of the number of digits. These numbers are a fascinating concept in number theory and a common starting point for beginners in programming. For example, in base 10:

Example:

153 = 1³ + 5³ + 3³ = 153

Key Properties:

  • An Armstrong number can exist in any number base.
  • In base 10, single-digit numbers (0 and 1) are considered Armstrong numbers.

Armstrong Number Examples

Here are some examples of Armstrong numbers in the decimal system:

  • 3-Digit Numbers:
    • 153: 1³ + 5³ + 3³ = 153
    • 370: 3³ + 7³ + 0³ = 370
    • 371: 3³ + 7³ + 1³ = 371
    • 407: 4³ + 0³ + 7³ = 407
  • 4-Digit Numbers:
    • 1634: 1⁴ + 6⁴ + 3⁴ + 4⁴ = 1634
    • 8208: 8⁴ + 2⁴ + 0⁴ + 8⁴ = 8208
    • 9474: 9⁴ + 4⁴ + 7⁴ + 4⁴ = 9474

Armstrong Numbers in Base 3

Even in other bases, Armstrong numbers exist. For example: 122 (base 3) = 1³ + 2³ + 2³ = 17 (decimal).

How to Check for Armstrong Numbers?

To determine whether a number is an Armstrong number, follow this algorithm:

Algorithm:

  1. Input: Take the number to be checked.
  2. Find Digits: Determine the number of digits (“n”).
  3. Process Each Digit:
    • Extract each digit using modulus (num % 10).
    • Raise the digit to the power of “n”.
    • Add the result to a running total.
    • Divide the number by 10 to move to the next digit.
  4. Compare: If the total equals the original number, it’s an Armstrong number.

Example Algorithm in Python:

num = int(input("Enter a number: "))
original = num
sum = 0
n = len(str(num))

while num > 0:
    digit = num % 10
    sum += digit ** n
    num //= 10

if sum == original:
    print(f"{original} is an Armstrong Number!")
else:
    print(f"{original} is not an Armstrong Number.")

Output Example:

Enter a number: 153

153 is an Armstrong Number!

Python Programs for Armstrong Numbers

Check a Specific Number:

num = int(input("Enter a number: "))
original = num
sum = 0
n = len(str(num))

while num > 0:
    digit = num % 10
    sum += digit ** n
    num //= 10

if sum == original:
    print(f"{original} is an Armstrong Number.")
else:
    print(f"{original} is not an Armstrong Number.")

Output Example:

Enter a number: 123

123 is not an Armstrong Number.

Find Armstrong Numbers in a Range:

t1 = int(input("Enter the minimum value: "))
t2 = int(input("Enter the maximum value: "))

print("Armstrong numbers in the range:")
for num in range(t1, t2 + 1):
    original = num
    sum = 0
    n = len(str(num))

    while num > 0:
        digit = num % 10
        sum += digit ** n
        num //= 10

    if sum == original:
        print(original)

Output Example:

Enter the minimum value: 100

Enter the maximum value: 500

Armstrong numbers in the range: 153, 370, 371, 407

N = int(input("Enter the number of Armstrong numbers to find: "))
count = 0
num = 1

print("The first", N, "Armstrong numbers are:")
while count < N:
    original = num
    sum = 0
    n = len(str(num))

    while num > 0:
        digit = num % 10
        sum += digit ** n
        num //= 10

    if sum == original:
        print(original)
        count += 1

    num += 1

Output Example:

Enter the number of Armstrong numbers to find: 5

The first 5 Armstrong numbers are: 1, 153, 370, 371, 407

Conclusion

In this blog, we explored the concept of Armstrong numbers, their properties, examples, and Python implementations. Armstrong numbers may not have real-world applications but are a fun and educational topic for programming practice. For more tutorials on Python and machine learning, stay tuned!

Also Read:

→ Explore this Curated Program for You ←

Avatar photo
Great Learning Editorial Team
The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.

Full Stack Software Development Course from UT Austin

Learn full-stack development and build modern web applications through hands-on projects. Earn a certificate from UT Austin to enhance your career in tech.

4.8 ★ Ratings

Course Duration : 28 Weeks

Cloud Computing PG Program by Great Lakes

Enroll in India's top-rated Cloud Program for comprehensive learning. Earn a prestigious certificate and become proficient in 120+ cloud services. Access live mentorship and dedicated career support.

4.62 ★ (2,760 Ratings)

Course Duration : 8 months

Scroll to Top