# Armstrong Number in Python | 2023

**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 the decimal number system, 153 is an armstrong number.

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

- What is armstrong number
- Armstrong Number with Examples
- Armstrong number logic
- Armstrong Number Algorithm
- Algorithm to check if the given number is Armstrong or not
- What is the Armstrong 4-digit number?
- How do I find my 4-digit Armstrong number?
- Printing a Series of Armstrong Numbers Using Python
- Python program to check armstrong number
- Armstrong number in python FAQs

**Armstrong Number with Examples**

As explained in the prior sections of this blog, a number that is composed of the digits, such that the sum of the cube of each of these digits is equal to the number itself, is referred to as an Armstrong number. Narcissistic numbers are another name for Armstrong numbers. Armstrong numbers are not applicable in the actual world. It is just another numerical puzzle, in reality. Take a look at the following examples of an Armstrong number.

Example-1: 153 13 + 53 + 33 = 153 Example-2: 370 33 + 73 + 01 = 370 Example-4: 371 33 + 73 + 13 = 371 Example-5: 407 43 + 03 + 73 = 407

Among single digit numbers 0 and 1 can be considered as Armstrong numbers. In any case, for the number to be an Armstrong number, the sum of its digits raised to the power of total number of digits. For instance, for a four or five digit number, we would check the sum of the fourth power of each digit or the sum of the fifth power of each digit, respectively.

**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 possess 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 is the number of digits in the number, and second the sum of the digits raised to the power of a number of digits. Let us look at the algorithm to gain a better understanding:

**Algorithm:**

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

**Methods to Find an Armstrong Number using Python**

There are different Python programs associated with finding an Armstrong number. You can check whether a given number is an Armstrong number or not. Alternatively, you can find all the Armstrong numbers within a specified range of numbers. We will go with both these approaches related to the identification of Armstrong numbers, in Python.

*To Check Whether the Given Number is Armstrong*

```
#For a three digit number
num = int(input('Enter the three digit number to be checked for Armstrong: '))
t = num
cube_sum = 0
while t!= 0:
k = t % 10
cube_sum += k**3
t = t//10
if cube_sum == num:
print(num, ' is an Armstrong Number')
else:
print(num, 'is not a Armstrong Number')
```

*Output:*

*Identifying the Armstrong Numbers In a Given Range*

```
t1 = int(input('Enter the minimum value of the range:'))
t2 = int(input('Enter the maximum value of the range:'))
n=len(str(t2))
print('The Armstrong numbers in this range are: ')
for j in range(t1, t2+1):
i=j
cube_sum = 0
while i!= 0:
k = i % 10
cube_sum += k**n
i = i//10
if cube_sum == j:
print(j)
```

*Output:*

**What is the Armstrong 4-digit number?**

An Armstrong four-digit number is nothing but a number that consists of four such digits whose fourth powers when added together give the number itself. Here are two examples given below.

*Example-1: *1634

1^{4} + 6^{4} + 3^{4} + 4^{4} = 1634

*Example-2: *8208

8^{4} + 2^{4} + 0^{4} + 8^{4} = 8208

**How do I find my 4-digit Armstrong number?**

Try out the following Python code to find all the 4-digit Armstrong numbers:

```
print('4-digit Armstrong numbers are as follows: ')
for j in range(1000, 9999): #Setting the loop range for 4-digit numbers
i=j
cube_sum = 0
while i!= 0:
k = i % 10
cube_sum += k**4
i = i//10
if cube_sum == j:
print(j)
```

*Output:*

**Algorithm to check if the given number is Armstrong or not**

You can adopt the following algorithm to check if the given number is Armstrong number or not

*Step-1: *Take the number to be checked as input from the user.* *

*Step-2: *Determine the number of digits in the number, say it is n.

*Step-3: *Extract each digit from the number using a loop. Now raise each of these digits to the power n.

*Step-4: *Keep adding the n^{th} power of these digits in a variable, say sum.

Step-5: Once, the sum of all the n^{th} power of the digits is obtained, the loop terminates and the, you can check if the value of sum is equal to the number itself. If both are equal, then the number will be called as an Armstrong number, else it is not an Armstrong number.

**Is 153 an Armstrong number?**

Yes, 153 is a three-digit Armstrong number as the sum of third powers of 1, 5, and 3 makes the number 153 itself.

1^{3} + 5^{3} + 3^{3} = 1 + 125 + 27

= 153

**Printing a Series of Armstrong Numbers Using Python**

**Python Program to Print the First “N” Armstrong Number**

```
N = int(input('For getting the first N Armstrong numbers, enter the value of N: '))
c = 0
j = 1
print('The Armstrong numbers in this range are: ')
while(c!=N):
n=len(str(j))
i=j
sum = 0
if(n>3):
while i!= 0:
k = i%10
sum += k**n
i = i//10
if sum == j:
c+=1
print(j)
else:
while i!= 0:
k=i%10
sum += k**3
i = i//10
if sum == j:
c+=1
print(j)
j+=1
```

*Output:*

**Python Program to Print a Series of Armstrong Numbers in a Pre-defined Range**

```
t1 = int(input('Enter the minimum value of the range:'))
t2 = int(input('Enter the maximum value of the range:'))
n=len(str(t2))
print('The Armstrong numbers in this range are: ')
for j in range(t1, t2+1):
#t1 and t2 define the range
i=j
cube_sum = 0
while i!= 0:
k = i % 10
cube_sum += k**n
i = i//10
if cube_sum == j:
print(j)
else:
print('There are no Armstrong numbers in this range')
```

*Output:*

**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. 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. *

** Armstrong number in python FAQs**

**What is an Armstrong number in python?**In Python, we can write a program that checks for an Armstrong number. An Armstrong number is defined as a number that consists of three such digits whose sum of the third powers is equal to the original number.

**Is 371 an Armstrong number?**Yes, 371 is an Armstrong number, as 3^{3} + 7^{3} + 1^{3} = 371

**What are Armstrong numbers in 1 to 100?**The Armstrong number that lies in the range from 1 to 100 is only 1.

**How do you find a number is Armstrong or not?**We find a number is Armstrong number or not by calculating the sum of the cubes of each of its digits. If the sum comes out to be same as the original number, it is said to be an Armstrong number, else it is not an Armstrong number.

**How 1634 is an Armstrong number?**Since 1634 is a four digit number, we can find out the sum of fourth power of each of its digits. Now, this sum turns out to be 1634.

1^{4} + 6^{4} + 3^{4} + 4^{4}

= 1 + 1296 + 81 + 256

= 1634

Hence, 1634 is an Armstrong number.

**Is 123 an Armstrong number?**No, 123 is not an Armstrong number, as-

1^{3} + 2^{3} +3^{3} is not equal to 123

**Is 370 an Armstrong number?**Yes, 370 is an Armstrong number.

**Is 407 A Armstrong number?**Yes, 407 is an Armstrong number.

**Why 153 is an Armstrong number?**153 is an Armstrong number because the sum of cubes of 1, 3, and 5 makes the number 153.

1^{3} + 5^{3} +3^{3}

= 1 + 125 + 27

= 153

Source : https://www.mygreatlearning.com/blog/artificial-intelligence/