Armstrong Number in Python: Explained with Examples

Updated on July 19, 2024

Article Outline

Armstrong Number is a special kind of number in Math. This number equals the sum of digits, each number raised to a power, For example, If you have a number like 153, It is an Armstrong Number Because 1^3 + 5^3 + 3^3 equals 153. Armstrong’s number is also called a narcissistic number or plenary number. In this article, we will check whether the number is Armstrong or not using Python language.

What is the Armstrong Number in Python?

The Armstrong Number in Python is the number that each number raised to power. The sum of digits is equal to that number. For example, 1^3+ 5^3+ 3^3 equals 153.

armstrong number

*Image
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure

Armstrong Number Logic

An Armstrong number is a simple mathematical number. This number has some property. Let’s see how we can find the Armstrong number.

 

  • Take any number from the user. For example, We will take 153 as a number and divide that number into 1, 5, and 3 digits.
  • In the next step, we will add each digit to a power equal to the total number of digits in the original number. In this example, we have three digits after adding each number has power, it will look like this 1 ^3, 5^3, and 3 ^3.
  • Now, we can calculate the result of each number by adding these raised digits: 1 ^ 3 = 1, 5^3 = 125, and 3 ^3 = 27.
  • Finally, we can add those numbers to see the result = 1 + `125 + 27 = 153.

Armstrong Number Algorithm

Armstrong’s Number is also called the Narcissistic number or plenary number. It is a special type of number in Mathematics. In this section, we will use the Armstrong number algorithm.  Here’s the Armstrong Number algorithm.

 

  • Take the integer input from the user in the Python program.
  • Count the Number of digits. If there is more than one digit, check “Is this number Armstrong or not?”
  • Iterate each digit through a loop and raise each digit to the power of the total number of digit
  • Then, the sum of those digits.
  • In this step, you have to check the sum of the iterated digits and your input number. If you compare those numbers, if they are equal, return “This is an Armstrong number; “ otherwise, return “This is not an Armstrong number.”

Check a three-digit number using a while loop

In this program, we will check whether the number is Armstrong or not using the while loop. We will check the three-digit number.

 

Source Code

 

# This code written by Neeraj Kumar number = int(input("Enter a number: ")) # initialize sum sum = 0 temp = number while temp > 0: digit = temp % 10 sum += digit ** 3 temp //= 10 if number == sum: print(sum,"is an Armstrong number") else: print(sum,"is not an Armstrong number")

Output

Enter a number: 153 153 is an Armstrong number

Check the n-digit number using recursion

In this method, we will check whether the number is Armstrong by using the recursion. Recursion is the technique where the function calls itself. The following program demonstrates the Recursion method.

 

Program

 

def count_digits(n): if n == 0: return 0 return 1 + count_digits(n // 10) def armstrong_recursive(n, num_digits): if n == 0: return 0 return (n % 10)**num_digits + armstrong_recursive(n // 10, num_digits) def is_armstrong(n): num_digits = count_digits(n) armstrong_sum = armstrong_recursive(n, num_digits) return armstrong_sum == n num = int(input("Enter a number to check if it's an Armstrong number: ")) if is_armstrong(num): print(num, "is an Armstrong number.") else: print(num, "is not an Armstrong number.")

 

Output

 

Enter a number to check if it's an Armstrong number: 153 153 is an Armstrong number.

Check the n-digit numbers using functions

In this section, we will check the Armstrong number in Python using a function. In this example, we created two functions: the sum and the count_digits_fun() function. The following program demonstrates the code.

 

Program

 

def count_digits_fun(n): i= 0 while n> 0 : n //= 10 i += 1 return i ; def sum(n): i = count_digits_fun(n) s = 0 while n > 0: digit = n%10 n //= 10 s += pow(digit,i) return s value = sum(number) ; if number == value: print(value,"is an Armstrong number") else: print(value,"is not an Armstrong number")

 

Output

Enter a number to check if it's an Armstrong number: 153 153 is an Armstrong number.

Check the n-digit numbers using a while loop

In this section, we will find the n-digit Armstrong number using the while. The following program demonstrates the n-digit Armstrong number using a while loop.

 

Program

 

number = int(input("Enter the number = ")) digits = len(str(number)) temp = number add_sum = 0 while temp != 0: k = temp % 10 add_sum += k**digits temp = temp//10 if add_sum == number: print('It is an Armstrong Number') else: print('It is not a Armstrong Number')

 

Output

 

Enter the number = 8208 It is an Armstrong Number

Conclusion

Armstrong number is an interesting and practical application in Mathematics. In this article, we learned how to create an Armstrong program in the Python Programming language. Implementing Armstrong in Python increases problem-solving skills in Python programming and builds logic implementation. We saw different approaches to building the Armstrong program, such as Recursion and loops. Recursion is the technique where the function calls itself, and the loop uses the iteration approach. If you want to explore Python programming, you can consider the Accelerator Program in Business Analytics and Data Science.

 

FAQs
In Python, we can check if a number is Armstrong by calculating the sum of digits raised to the power of the number and comparing that number with the original number. If this is equal, it is Armstrong; otherwise, it is not Armstrong.
No, the negative number cannot be an Armstrong number because the definition of Armstrong only applies to non-negative numbers.
The iterative approach is generally more efficient for finding Armstrong numbers in Python.
In Python, we can check if a number is Armstrong by calculating the sum of digits raised to the power of the number and comparing that number with the original number. If this is equal, it is Armstrong; otherwise, it is not Armstrong.
Yes, Armstrong's number can have any number of digits: a single-digit number, a two-digit number, a number with more than two digits, and so on.

Updated on July 19, 2024

Link
left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.
Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

|

Sitemap

© 2024 Hero Vired. All rights reserved