- What is armstrong number
- Armstrong number logic
- Armstrong number examples
- Armstrong Number Algorithm
- 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.

1^3+5^3+3^3=153

## 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 ?

0^1=0

1^1=1

1^3+5^3+3^3=153

3^3+7^3+0^3=370

3^3+7^3+1^3=371

4^3+0^3+7^3=407

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

#### Algorithm:

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
cnt=0
while(num>0):
cnt=cnt+1
num=num//10
while num2>0:
rem = num2% 10
sum1 += rem ** cnt
num2//= 10
if(num_original==sum1):
print('Armstrong!!')
else:
print('Not Armstrong!')
```

Output

Enter a number : 153

Armstrong!!

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. Let us know what you think in the comment section below.

1