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

Python Project

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



for x in range (p) :



if my_sum == tot:

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

else :

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

Follow me on Twitter..

  1. No comments yet.
  1. December 12, 2018 at 9:35 am

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