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:
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
- 153 is an Armstrong number because:
- 1^3 + 5^3 + 3^3 = 153
- 371 is an Armstrong number because:
- 3^3 + 7^3 + 1^3 = 371
- 407 is an Armstrong number because:
- 4^3 + 0^3 + 7^3 = 407
- 1634 is an Armstrong number because:
- 1^4 + 6^4 + 3^4 + 4^4 = 1634
- 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:
input()takes a number from the user, andint()converts it into an integer.- The variable
tempstores a copy of the original number so that we can extract its digits without changingnum. len(str(num))calculates the total number of digits.- The
whileloop runs until all digits have been extracted. temp % 10extracts the last digit of the number.digit ** powercalculates the digit raised to the required power.total += digit ** poweradds the calculated value to the running sum.temp //= 10removes the last digit.- 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?
| Iteration | temp | Extracted digit | Digit raised to power 3 | Total |
|---|---|---|---|---|
| 1 | 153 | 3 | 3^3=27 | 27 |
| 2 | 15 | 5 | 5^3=125 | 152 |
| 3 | 1 | 1 | 1^3=1 | 153 |
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:
- Read an integer from the user.
- Calculate the number of digits using
len(str(num)). - Initialize
totalto zero. - Use a
forloop to iterate through each character in the string representation of the number. - Convert each character into an integer using
int(digit). - Raise each digit to the power of the total number of digits.
- Add the calculated values to
total. - Compare
totalwith the original number.
For 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:
- Define a function named
is_armstrong()that accepts a number as an argument. - Calculate the number of digits using
len(str(num)). - Iterate through each digit using a
forloop. - Calculate the power of each digit and add it to the total.
- Use
return num == totalto returnTrueif both values are equal; otherwise, returnFalse. - 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:
- Convert the input number into a string using
str(num). - Use list comprehension to extract and convert each digit into an integer.
- Calculate the total number of digits using
len(digits). - Use another list comprehension to calculate each digit raised to the required power.
- The
sum()function adds all the calculated values. - Compare the result with the original number.
For 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:
- Take the starting and ending numbers as input.
- Use the outer
forloop to iterate through every number in the specified range. - Calculate the number of digits in the current number.
- Use the inner
forloop to extract each digit and calculate its power. - Add the powered digits to obtain the total.
- Compare the total with the current number.
- If both values are equal, print that number.
This program displays all Armstrong numbers within the specified range.