Home > Problem, Projects/Experiments, Python > Python Project

Python Project




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):

tot=num

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))

my_sum=0

count = count +1

else:

my_sum=0

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



Follow me on Twitter..

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s