Blog

Blog

How To Best Implement Armstrong Number In Python?

Youtube banner Logo -Armstrong Number

Armstrong Number In Python

What is Armstrong’s 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 they 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

An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. For example, 371 is an Armstrong number because 33 + 73 + 1**3 = 371.

To check if a number is an Armstrong number in Python, you can use a simple function like this:

def is_armstrong_number(number):
    num_str = str(number)
    num_digits = len(num_str)
    sum = 0
for digit in num_str:
        sum += int(digit)**num_digits
    return sum == number

You can then test this function by calling it with different numbers, like this:

print(is_armstrong_number(371))  # True
print(is_armstrong_number(123))  # False
print(is_armstrong_number(407))  # True

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 Armstrong numbers. In any case, for the number to be an Armstrong number, the sum of its digits is raised to the power of a 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 its fourth power to get the desired result.

1634, 8208, and 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 the second is 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 the 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:

b1l6L2aEdNVpzTEP4H6GnzCjkaY qorvvSHDye t5HqlzQwMciT1dF5qadb2ZnSUjkumlBKCQfjOO 89A cUOKAPJ7ZhOQ3EzvohZolrgPk17s9TlhswoC20OHvmMObxgNjrpZjAQb10 FOFty01IEbVE6O
How To Best Implement Armstrong Number In Python? 8
T y UEUF6lmiK6Xjbl5CiSIte41C2wMboCEK5fPm C99sJRROzWpXydvIZp

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:

PJVKofSMrMjvk5Y8VeX8f1C2netTJ61WDtKExrQ8eS4MR4 TUZdHLYICHtvYVMSBsDIgmAT5KnHWiij rEYa6AOonuir5vS9FhG0puZp4l mJbvRXgW uV3HWc06TpDVY5xLRulrZ3CzTKo1wNr0ri1DtdRIRdr goNJf3A4yuLnTvq7WRSIe2UsSHcZA0e4

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

14 + 64 + 34 + 44 = 1634

Example-2: 8208

84 + 24 + 04 + 84 = 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:

mphETuN n9htSknyYcQySjwV4ZCxWyb PrKM0sG1WxQoaEyqikoCVLIGtbYqtwpS1WWuWAFnz8RN2YazFSyc9ufS5V6NEvr pCwJH3nc4zsrXAaJTXbDN6 pDXf6MG7jqtZ3OCqOyuvUZQ5AlKhTMNeUF3Py5c b6FGgepkMhn2W puZt D8kHe8qmLQ6dX6wigKJw

Algorithm to check if the given number is Armstrong or not

You can adopt the following algorithm to check if the given number is an 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 nth power of these digits in a variable, say sum.

Step-5: Once, the sum of all the nth power of the digits is obtained, the loop terminates and, you can check if the value of the sum is equal to the number itself. If both are equal, then the number will be called 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. 

13 + 53 + 33 = 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+=

Output:

7d2gROLTdXY9mxxSIoA8daJVVQo5IJaGpii74WdwPRQRYrZkBarSfdOX9PVi FXLcPCuROlmerqynEr2Jdbk7K5MulfGsba4y2IT4u2KUWi8mqVZNHANjBvQ 1 n8rxWygCWKWM2dupRkLrMlAOhHj5Tg3BZ SYsAvnzi93 2CJDzW7E2vMOFZ23a7n 5Jm4x 10EA

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:

uCYUD1taItWmnLRJkaipaRPYlJRzgLPA MQyHGZvkEv4D7PoNtfdWMMf37bTB65H1sfJBGiL0omk3iU nU3PYkM4Ut5pIvNUnq60hh XkYLcac lX6dP r 9VJgNpGb6hSjlc2ib7voIHXKBxhje4TEzn8GmJ1Kor QFvcVr

Python program to check Armstrong Number

We will understand and implement 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!

TEST THE CODE

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.

Armstrong number in python FAQs

Is 371 an Armstrong number?

Yes, 371 is an Armstrong number.

An Armstrong number is a number that is equal to the sum of its own digits when each digit is raised to the power of the number of digits. For example, 371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371.

Here is the Python code that demonstrates this:

def is_armstrong_number(num):
    # Convert the number to a string and get the length
    num_str = str(num)
    length = len(num_str)

    # Initialize a variable to store the sum
    sum = 0

    # Iterate through each digit in the number
    for digit in num_str:
        # Convert the digit to an integer and raise it to the power of the length
        sum += int(digit)**length

    # Return True if the sum is equal to the original number, False otherwise
    return sum == num

print(is_armstrong_number(371))  # prints True

What are Armstrong numbers from 1 to 100?

An Armstrong number is a number that is equal to the sum of its own digits when each digit is raised to the power of the number of digits. For example, 371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371.

Here is a Python function that can determine whether a number is an Armstrong number:

def is_armstrong_number(num):
    # Convert the number to a string and get the length
    num_str = str(num)
    length = len(num_str)

    # Initialize a variable to store the sum
    sum = 0

    # Iterate through each digit in the number
    for digit in num_str:
        # Convert the digit to an integer and raise it to the power of the length
        sum += int(digit)**length

    # Return True if the sum is equal to the original number, False otherwise
    return sum == num

To get a list of Armstrong numbers from 1 to 100, you can use a loop to iterate through the numbers and check if each one is an Armstrong number using the function above:

armstrong_numbers = []

for num in range(1, 101):
    if is_armstrong_number(num):
        armstrong_numbers.append(num)

print(armstrong_numbers)

This will print the following list of Armstrong numbers from 1 to 100:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407]

How do you find a number is Armstrong or not?

We find whether a number is an Armstrong number or not by calculating the sum of the cubes of each of its digits. If the sum comes out to be the 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 the fourth power of each of its digits. Now, this sum turns out to be 1634.

14 + 64 + 34 + 44

= 1 + 1296 + 81 + 256

= 1634

Hence, 1634 is an Armstrong number.

Is 123 an Armstrong number?

No, 123 is not an Armstrong number, as-

13 + 23 +33 is not equal to 123

Is 370 an Armstrong number?

Yes, 370 is an Armstrong number. An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. For example, 371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371. 370 is also an Armstrong number because 3^3 + 7^3 + 0^3 = 370.

Is 407 A Armstrong’s number?

Yes, 407 is an Armstrong number. An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. For example, 371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371. 407 is also an Armstrong number because 4^3 + 0^3 + 7^3 = 407.

Why 153 is an Armstrong number?

153 is an Armstrong number because it satisfies the condition for being an Armstrong number, which is that the sum of the digits of the number raised to the power of the number of digits must equal the original number.

For example, 153 has 3 digits (1, 5, and 3), and if you raise each of these digits to the power of 3 and add them up, you get:

1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

Therefore, 153 is an Armstrong number.

Here is the Python code that demonstrates this:

def is_armstrong_number(num):
    # Convert the number to a string and get the length
    num_str = str(num)
    length = len(num_str)

    # Initialize a variable to store the sum
    sum = 0

    # Iterate through each digit in the number
    for digit in num_str:
        # Convert the digit to an integer and raise it to the power of the length
        sum += int(digit)**length

    # Return True if the sum is equal to the original number, False otherwise
    return sum == num

print(is_armstrong_number(153))  # prints True
Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare

Subscribe to Newsletter

Stay ahead of the rapidly evolving world of technology with our news letters. Subscribe now!