Mathematics and programming when combined make a very deadly combination. As the ability to solve complex mathematical questions in itself is a great deal. But when one has to do the same thing by writing up a code, things get somewhat more complicated. Not to mention the language your coding in also determines whether it’s going to be easy to difficult. Well, Armstrong numbers are very well known in the mathematical world. Therefore today we’re going to write a program to check if the given input number is Armstrong Number or not using Python.
What are Armstrong Numbers?
- Let us consider Number
Nbeing the no digits in
Xwill be said as the Armstrong number if the sum of each digit of
Xis raised with order
- Eg: 153 is the Armstrong No.
1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3 = 153
Also Read: Check If Two Strings Are Anagram in Python
What’s The Approach?
- Firstly in the input number (
X), we will
Find The Number Of Digits(
N) so that we can determine the order.
- Once we find the order, then for each digit (
R) we will compute
- After that, we will perform the addition of the computed values.
- If the addition equals
N, then the number is Armstrong otherwise it’s Not.
Python Program To Check Armstrong Number
# Python program to determine whether the number is # Armstrong number or not # Function to calculate x raised to the power y def power(x, y): if y==0: return 1 if y%2==0: return power(x, y/2)*power(x, y/2) return x*power(x, y/2)*power(x, y/2) # Function to calculate order of the number def order(x): # variable to store of the number n = 0 while (x!=0): n = n+1 x = x/10 return n # Function to check whether the given number is # Armstrong number or not def isArmstrong (x): n = order(x) temp = x sum1 = 0 while (temp!=0): r = temp%10 sum1 = sum1 + power(r, n) temp = temp/10 # If condition satisfies return (sum1 == x) # Driver Program x = 153 print(isArmstrong(x)) x = 1253 print(isArmstrong(x))