armstrong in python
  1. What is armstrong number
  2. Armstrong number logic
  3. Armstrong number examples
  4. Armstrong Number Algorithm
  5. Python program to check armstrong number

What is Armstrong Number?

The armstrong number, also known as the narcissist number, is of special interest to beginners. It kindles the curiosity of programmers just setting out with learning a new programming language. In number theory, an armstrong number in a given number base b is a number that is the sum of its own digits each raised to the power of the number of digits.
To put it simply, if I have a 3-digit number then each of the digits is raised to the power of three and added to obtain a number. If the number obtained equals the original number then, we call that armstrong number. The unique property related to the armstrong numbers is that it can belong to any base in the number system. For example, in decimal number system, 153 is an armstrong number.

Armstrong Number Logic

Let us understand the logic of obtaining an armstrong number.
Consider the 6 -digit number 548834 .Calculate the sum obtained by adding the following terms.
5^6 + 4^6 + 8^6 + 8^6 + 3^6 + 4^6 = 548834
We obtain the same number when we add the individual terms. Therefore, it satisfies the property.

122 in base 3 is 1^3+2^3 + 2^3=17
122 base 3 is equivalent to 2*1+2*3+1*9=17

Armstrong number examples

0, 1, 153, 370, 371 and 407 . Why ?

4 digit armstrong number

For a 4 digit number, every digit would be raised to their fourth power to get the desired result.
1634, 8208, 9474 are a few examples of a 4 digit armstrong number.

Are there any applications of Armstrong Numbers?

These numbers posses a unique dimension, but unfortunately they are not of any practical use. Programmers who learn new languages tend to begin with puzzles which help them grasp the concepts of the language they are learning better.

Armstrong Number Algorithm

We need two parameters to implement armstrong numbers. One being the number of digits in the number, and second the sum of the digits raised to the power of number of digits. Let us look at the algorithm to gain a better understanding:


1. The number of digits in num is found out
2. The individual digits are obtained by performing num mod 10, where mod is the remainder operation.
3. The digit is raised to the power of the number of digits and stored
4. Then the number is divided by 10 to obtain the second digit.
5. Steps 2,3 and 4 are repeated until the value of num is greater than 0
6. Once num is less than 0, end the while loop
7. Check if the sum obtained is same as the original number
8. If yes, then the number is an armstrong number

Python program to check Armstrong Number

We will understand implementing the above algorithm using Python.

num = int(input('Enter a number: '))
num_original =num2=num
sum1 = 0


while num2>0:
   rem = num2% 10
   sum1 += rem ** cnt
   num2//= 10

    print('Not Armstrong!')

Enter a number : 153

Enter a number : 134
Not Armstrong!


num_original stores the initial value of the input, whereas num stores the number of digits in the input. Using the variable num_2, we compute the sum of individual digits raised to the power of the count variable respectively. Finally, we compare the original number and the sum obtained to check for the property of an armstrong number

In this article we considered the Armstrong Number, the algorithm and its implementation. For more tutorials on python and machine learning stay tuned. You can also take up AI ML courses and power ahead in your career. Let us know what you think in the comment section below.



Please enter your comment!
Please enter your name here

1 + eighteen =