Python Programs for Armstrong Numbers
1. Python Program to Check Armstrong Number
In this program, we first define two functions: order(num) and is_armstrong(num).
The order(num) function calculates the number of digits in the given number. We do this by dividing the number by 10 repeatedly until we get a quotient of 0, and incrementing a count variable each time we do this.
The is_armstrong(num) function checks if the given number is an Armstrong number or not. We initialize two variables: sum and temp. The temp variable is used to store a copy of the original number so that we can manipulate its digits without losing the original number. The sum variable is used to store the sum of the cubes of the digits.
We then calculate the number of digits in the given number using the order(num) function. We use a while loop to extract each digit of the number and add its cube to the sum variable. We then divide the number by 10 and repeat until we have processed all digits.
Finally, we check if the sum of the cubes of the digits is equal to the original number. If it is, we return True and print that the number is an Armstrong number. If not, we return False and print that the number is not an Armstrong number.
We take user input using the input() function, and then call the is_armstrong(num) function to check if the number is an Armstrong number or not. We then print the result using the print() function.
Input Program :
# Python program to check Armstrong number
# Function to calculate the order of the number
def order(num):
# Initialize variables
count = 0
temp = num
while temp > 0:
count += 1
temp //= 10
return count
# Function to check if the number is Armstrong or not
def is_armstrong(num):
# Initialize variables
sum = 0
temp = num
n = order(num)
while temp > 0:
digit = temp % 10
sum += digit ** n
temp //= 10
# Return True if the sum of the cubes of the digits is equal to the original number, else False
return num == sum
# Take user input
num = int(input("Enter a number: "))
# Check if the number is Armstrong or not
if is_armstrong(num):
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
Output of Program :
Enter a number: 5
5 is an Armstrong number
&
Enter a number: 24
24 is not an Armstrong number
__________________________________________________________________________
2. Python Program to Find Armstrong Number in an Interval
In this program, we first define two functions: order(num) and is_armstrong(num). These functions are the same as in the previous program, so we won't explain them again here.
We then take input from the user for the lower and upper limits of the interval. We initialize an empty list to store the Armstrong numbers that we find in the interval.
We then loop through each number in the interval, and for each number, we check if it is an Armstrong number using the is_armstrong(num) function. If it is, we append it to the armstrong_nums list.
Finally, we print all the Armstrong numbers found in the interval using a for loop and the print() function.