Archive for December 12, 2018

Python Project

December 12, 2018 Leave a comment

Python: Armstrong Numbers

Check for Armstrong number in a range

In our last post (Check if a Number is Armstrong) we wrote a codes to check a given number and see whether it is an Armstrong or Not.

Today, we want to go through a set of numbers and see how many Armstrong numbers are there.

Before calling the ‘armstrong_in_range()’ and just to keep the code as less as we can, I assume the two numbers has the same number of digits, also I am getting the power (p) and the range numbers (n1, n2) out-side of this function and passing all as variables.

def armstrong_in_range(p,n1,n2):

my_sum = 0

count = 0

for num in range (n1,n2):


for arm in range(p):

my_sum = my_sum + ((num % 10)**p)

num = num // 10

if my_sum == tot:

print(‘\nThe number {} is Armstrong.’.format(tot))


count = count +1



print(‘\nWe have {} armstrong number(s)’.format(count))

Follow me on Twitter..