Home » Python Program » Armstrong number Program in Python with Explanation

Armstrong number Program in Python with Explanation

Last updated:

Support this post with a reaction:

5,176 views

An Armstrong number is a number that is equal to the sum of its digits, where each digit is raised to the power of the total number of digits in that number. For example, 153 is an Armstrong number because it contains three digits and 1^3 + 5^3 + 3^3 = 153. In Python, we can check whether a number is an Armstrong number by extracting its individual digits, calculating their powers, and comparing the sum with the original number.

What is Armstrong Number?

An Armstrong number is a number that is equal to the sum of its individual digits raised to the power of the total number of digits in that number.

It is also known as a Narcissistic number.

For an n-digit number, the Armstrong number condition is:

N=d1n+d2n+d3n+⋯+dnn N=d_1^n+d_2^n+d_3^n+\cdots+d_n^n

Where:

  • N represents the original number.
  • d1,d2,…,dn represent its individual digits.
  • n represents the total number of digits.

A Python program to check an Armstrong number determines whether a given number is equal to the sum of its digits, where each digit is raised to the power of the total number of digits.

For example, 153 is an Armstrong number because it contains three digits, and the sum of the cubes of its digits is equal to the original number:

Lets see it practically by calculating it,

First calculate the cube of its each digits

  1^3 = (1*1*1) = 1

  5^3 = (5*5*5) = 125

  3^3= (3*3*3) = 27

Now add the cube 

  1+125+27 = 153

It means 153 is an Armstrong number.

Some examples of Armstrong numbers

  1. 153 is an Armstrong number because:
    • 1^3 + 5^3 + 3^3 = 153
  2. 371 is an Armstrong number because:
    • 3^3 + 7^3 + 1^3 = 371
  3. 407 is an Armstrong number because:
    • 4^3 + 0^3 + 7^3 = 407
  4. 1634 is an Armstrong number because:
    • 1^4 + 6^4 + 3^4 + 4^4 = 1634
  5. 8208 is an Armstrong number because:
    • 8^4 + 2^4 + 0^4 + 8^4 = 8208

In this post, we will explore different logic to write a Python program that checks if a given number is an Armstrong number.

There are several ways to check whether a number is an Armstrong number in Python. Let’s understand each approach with code, output, and explanation.

Method 1: Python Program to Check Armstrong Number Using While Loop

This approach uses a while loop to extract individual digits from a number and calculate their powers.

Python code:

num = int(input("Enter a number: "))

temp = num
power = len(str(num))
total = 0

while temp > 0:
    digit = temp % 10
    total += digit ** power
    temp //= 10

if num == total:
    print(num, "is an Armstrong number")
else:
    print(num, "is not an Armstrong number")

Output:

Enter a number: 153
153 is an Armstrong number

Explanation of program:

  1. input() takes a number from the user, and int() converts it into an integer.
  2. The variable temp stores a copy of the original number so that we can extract its digits without changing num.
  3. len(str(num)) calculates the total number of digits.
  4. The while loop runs until all digits have been extracted.
  5. temp % 10 extracts the last digit of the number.
  6. digit ** power calculates the digit raised to the required power.
  7. total += digit ** power adds the calculated value to the running sum.
  8. temp //= 10 removes the last digit.
  9. Finally, the calculated sum is compared with the original number.

If both values are equal, the number is an Armstrong number; otherwise, it is not.

Dry Run: How does the while loop work for 153?

IterationtempExtracted digitDigit raised to power 3Total
115333^3=2727
21555^3=125152
3111^3=1153

After the loop terminates:

  • Original number = 153
  • Calculated sum = 153

Since both values are equal, 153 is an Armstrong number.

Method 2: Python Program to Check Armstrong Number Using For Loop

In this approach, we convert the number into a string and use a for loop to access each digit.

Python code:

num = int(input("Enter a number: "))

power = len(str(num))
total = 0

for digit in str(num):
    total += int(digit) ** power

if num == total:
    print(num, "is an Armstrong number")
else:
    print(num, "is not an Armstrong number")

Output:

Enter a number: 371
371 is an Armstrong number

Explanation of Program:

  1. Read an integer from the user.
  2. Calculate the number of digits using len(str(num)).
  3. Initialize total to zero.
  4. Use a for loop to iterate through each character in the string representation of the number.
  5. Convert each character into an integer using int(digit).
  6. Raise each digit to the power of the total number of digits.
  7. Add the calculated values to total.
  8. Compare total with the original number.

For 371:

33+73+13=27+343+1=371 3^3+7^3+1^3=27+343+1=371

Therefore, 371 is an Armstrong number.

Method 3: Python Program to Check Armstrong Number Using a Function

We can also create a user-defined function to check whether a number is an Armstrong number.

This approach makes the program reusable and easier to maintain.

Python code:

def is_armstrong(num):
    power = len(str(num))
    total = 0

    for digit in str(num):
        total += int(digit) ** power

    return num == total


num = int(input("Enter a number: "))

if is_armstrong(num):
    print(num, "is an Armstrong number")
else:
    print(num, "is not an Armstrong number")

Output:

Enter a number: 407
407 is an Armstrong number

Explanation of Program:

  1. Define a function named is_armstrong() that accepts a number as an argument.
  2. Calculate the number of digits using len(str(num)).
  3. Iterate through each digit using a for loop.
  4. Calculate the power of each digit and add it to the total.
  5. Use return num == total to return True if both values are equal; otherwise, return False.
  6. Call the function and display the appropriate message.

Method 4: Python Program to Check Armstrong Number Using List Comprehension

List comprehension provides a shorter way to calculate the sum of the powered digits.

Python code:

num = int(input("Enter a number: "))

digits = [int(digit) for digit in str(num)]
power = len(digits)

total = sum([digit ** power for digit in digits])

if num == total:
    print(num, "is an Armstrong number")
else:
    print(num, "is not an Armstrong number")

Output:

Enter a number: 1634
1634 is an Armstrong number

Explanation of Program:

  1. Convert the input number into a string using str(num).
  2. Use list comprehension to extract and convert each digit into an integer.
  3. Calculate the total number of digits using len(digits).
  4. Use another list comprehension to calculate each digit raised to the required power.
  5. The sum() function adds all the calculated values.
  6. Compare the result with the original number.

For 1634:

14+64+34+44 1^4+6^4+3^4+4^4

=1+1296+81+256=1634 =1+1296+81+256=1634

Therefore, 1634 is an Armstrong number.

Method 5: Python Program to Print Armstrong Numbers in a Given Range

Sometimes, instead of checking a single number, we need to find all Armstrong numbers between two given numbers.

We can solve this problem using nested loops.

Python code:

start = int(input("Enter starting number: "))
end = int(input("Enter ending number: "))

print("Armstrong numbers are:")

for num in range(start, end + 1):
    power = len(str(num))
    total = 0

    for digit in str(num):
        total += int(digit) ** power

    if num == total:
        print(num)

Output:

Enter starting number: 1
Enter ending number: 1000

Armstrong numbers are:
1
2
3
4
5
6
7
8
9
153
370
371
407

Explanation of Program:

  1. Take the starting and ending numbers as input.
  2. Use the outer for loop to iterate through every number in the specified range.
  3. Calculate the number of digits in the current number.
  4. Use the inner for loop to extract each digit and calculate its power.
  5. Add the powered digits to obtain the total.
  6. Compare the total with the current number.
  7. If both values are equal, print that number.

This program displays all Armstrong numbers within the specified range.