Home » Python Tutorial » Armstrong Number In Python

# Armstrong Number In Python

Computer programs can do many fun stuff. With python being one of the most popular out there, there have been attempts to try out different ideas in the language. One of the frequent requests gotten from programmers is how to write a computer program that checks for Armstrong number in Python.

Have you ever come across a number and where the sum of the digits raised to the power of total number of digits, is equal to the number itself. Such a number is called Armstrong numbers.

If xyz is an Armstrong number, mathematically,

The power is 3 because the number xyz contains 3 digits.

Going forward in this tutorial, you will understand the key idea behind obtaining an Armstrong number and how  we can check if a number is an Armstrong number in Python

Let’s dive in.

## What is an Armstrong Number?

A number can be referred to as an Armstrong number if it is equal to the sum of each digit to the power of the total number of digits is equal to the number. It is also called a narcissist number. One unique property of the Armstrong number is that it can belong to any base. This concept is quite intriguing especially for beginners in python because thinking about Armstrong numbers helps stimulate the curiosity trait in computer programming.

## Some Examples

Armstrong numbers are mostly 3-digit and 4-digit numbers. Examples of 3- digits Armstrong numbers are 153, 370, 371, and 407. Here’s why.

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

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

371: 3^3 + 7^3 + 1^3 = 3 71

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

Also, examples of 4-digit Armstrong number are 9474, 8208, 1634 and so on.

In the same vein,

9^4 + 4^4 + 7^4 + 4^4 = 9474

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

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

## Checking for Armstrong Number in Python

Two major concepts are needed to write a code for checking Armstrong’s number. First, you’d need to understand If/else statement (a piece of code used when you want an action to be done only if some defined conditions are satisfied). Secondly, you’d need to understand while loop (a piece of code used when an action should be done repeatedly until some defined condition is satisfied).

To check for an Armstrong number in python, you will need to calculate the sum of the cube of each digit (if the number is a 3-digit number). This is done by initializing the sum to 0 and then obtaining each digit number by using the modulus operator (%). Thereafter, you find the cubes using the exponent operator. Finally, you compare the original number with the sum gotten. If the two numbers are the same, then it an Armstrong number. The process described above is implemented in Python with the block of codes below.

``````# A Python program to check if the number given is an Armstrong number or not
#receive the input number from the user and typecast it into an integer
number = int(input("Enter a 3-digit number: "))
armstrong = number
#declare a variable that hold the sum of numbers
sum = 0
#declare a variable to hold the armstrong number

#find the sum of the cube of each digit
while armstrong > 0:
digit = armstrong % 10
sum += digit ** 3
armstrong //= 10

#Finally, we display the result

if number == sum:
print(number, "is an Armstrong number")
else:
print(number, "is not an Armstrong number")
``````

Output:

``````Enter a number: 213
213 is not an Armstrong number``````

Let’s try out a palindrome this time. Say 407.

``````Enter a 3-digit number: 407
407 is an Armstrong number``````

Exactly the result we expected. In conclusion, we looked at the concept of the Armstrong number and explained what an Armstrong number is. We went ahead to then write a program that checks for Armstrong number in Python.

## Some FAQs

What is an Armstrong number in python?

A 3-digit number is an Armstrong number if the sum of the cubes of i=each digit is equal to the number. If its a 4-digit number, the sum of the 4th power of each digit is equal to the number.

How to find Armstrong number in python?

1. Take input from the user, and store it as ‘number’

2. Set the input to be a variable, ‘temp’

3. Initialize a variable set to 0, that holds the sum of each digit

4. While temp >0, each digit = floor of 10. Increase the sum variable by the cube of each digit. Divide the number by 10.

5. If the number == sum, it is an Armstrong number. Else, it is not.

Shares