Archive
Python Project: IF N an Armstrong
Python: Armstrong Numbers
Check if a Number is Armstrong
In late nineties, I was programming using Pascal Language and I was very passionate to convert some mathematical syntax into codes to fine the result, although some of them were very easy; but the goal was to write the codes.
Today, we are attempted to write a code in Python to check whether a number is an Armstrong or Not. First let’s ask:what is Armstrong number?Answer: If we assume we have a number (num = 371), 371 is an Armstrong because the sum of each digits to the power of (number of the digits) will be the same. That’s mean 371 is a three digits so the power (p=3) so:
3**3 = 27
7**3 = 343
1**3 = 1
then (27+343+1) = 371. … So 371 is an Armstrong Number.
In wikipedia:
Armstrong also known as a pluperfect digital invariant (PPDI) or the Narcissistic number is a number that: the sum of its own digits each raised to the power of the number of digits equal to the number its self.
# Function to check whether a number is Armstrong or Not.
def is_it_armstrong(num):
p= len(str(num)) # First: we get the power of the number
my_sum=0
tot=num
for x in range (p) :
my_sum=my_sum+((num%10)**p)
num=num//10
if my_sum == tot:
print(‘\nThe number {} is Armstrong.’.format(tot))
else :
print(‘\nThe number {} is NOT Armstrong.’.format(tot))