Python Program to Check if Number is Armstrong
Python Program to Check if Number is Armstrong
An n-digit number abcd..
is said to be an Armstrong number if it obeys the following condition:
abcd.. = pow(a,n) + pow(b,n) + pow(c,n) + ...
For example:
153 = pow(1,3) + pow(5,3) + pow(3,3) = 1 + 125 + 27 = 153
In this tutorial, we will write a Python function to check if a given number is an Armstrong number and explore various examples.
Python Armstrong Function
The following function armstrong()
takes a number as an argument and checks if the number is Armstrong or not. The function returns True
if the given number is Armstrong, or False
otherwise.
def armstrong(num):
num = str(num)
n = len(num)
result = 0
# Compute sum of powers
for i in num:
result += pow(int(i), n)
# Early exit if result exceeds the number
if result > int(num):
break
# Check if the number equals the sum of powers
return int(num) == result
Explanation:
- The function converts the number to a string to iterate over its digits.
- The length of the number (n) is calculated using
len(num)
. - A for loop calculates the sum of each digit raised to the power of
n
. - The
if result > int(num)
condition optimizes the function by exiting early if the computed result exceeds the original number. - The function checks if the sum equals the original number and returns
True
if they match; otherwise, it returnsFalse
.
Example 1: Check if a Given Number is Armstrong
In this example, we read a number from the user and check if it is an Armstrong number.
Python Program
def armstrong(num):
num = str(num)
n = len(num)
result = 0
for i in num:
result += pow(int(i), n)
if result > int(num):
break
return int(num) == result
# Read the number from the user
num = input("Enter a number: ")
result = armstrong(num)
print(f"Is {num} an Armstrong Number? {result}")
Explanation:
- The user enters a number as input, which is passed to the
armstrong()
function. - The function computes if the number is Armstrong and returns a boolean result.
- The program displays the result to the user.
Output:
Enter a number: 153
Is 153 an Armstrong Number? True
Example 2: Check Armstrong Numbers for Multiple Inputs
This example demonstrates checking Armstrong numbers for a list of inputs.
Python Program
def armstrong(num):
num = str(num)
n = len(num)
result = 0
for i in num:
result += pow(int(i), n)
if result > int(num):
break
return int(num) == result
# List of numbers to check
numbers = [153, 9474, 9475, 370]
for num in numbers:
print(f"Is {num} an Armstrong Number? {armstrong(num)}")
Explanation:
- A list of numbers is defined for checking.
- The program iterates through each number in the list and checks if it is an Armstrong number using the
armstrong()
function. - Results are printed for each number.
Output:
Is 153 an Armstrong Number? True
Is 9474 an Armstrong Number? True
Is 9475 an Armstrong Number? False
Is 370 an Armstrong Number? True
Example 3: Armstrong Numbers in a Range
This example finds all Armstrong numbers within a specified range.
Python Program
def armstrong(num):
num = str(num)
n = len(num)
result = 0
for i in num:
result += pow(int(i), n)
if result > int(num):
break
return int(num) == result
# Find Armstrong numbers in a range
start, end = 100, 10000
print(f"Armstrong numbers between {start} and {end}:")
for num in range(start, end):
if armstrong(num):
print(num, end=" ")
Explanation:
- The range of numbers is specified using
start
andend
. - The program iterates through the range and checks each number using the
armstrong()
function. - Armstrong numbers are printed in a single line, separated by spaces.
Output:
Armstrong numbers between 100 and 10000:
153 370 371 407 1634 8208 9474
Summary
In this tutorial, we explored how to check if a number is an Armstrong number using Python. We covered:
- Writing a function to check for Armstrong numbers.
- Examples for single and multiple inputs.
- Finding Armstrong numbers within a specified range.
Each method provides insights into solving related problems, emphasizing flexibility and efficiency.