Internship Assurance

DevOps & Cloud Engineering

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.

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.

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’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.”

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

** **

**Also Read:** Armstrong Number program

in C

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

** **

**Output**

** **

Internship Assurance

DevOps & Cloud Engineering

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

**Output**

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

** **

**Output**

** **

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

How can I check if a number is an Armstrong number 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.

Can negative numbers be Armstrong numbers in Python?

No, the negative number cannot be an Armstrong number because the definition of Armstrong only applies to non-negative numbers.

Which approach, iteration or recursion, is more efficient for finding Armstrong numbers in Python?

The iterative approach is generally more efficient for finding Armstrong numbers in Python.

What is an example of Armstrong’s number?

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.

Can Armstrong numbers have more than three digits?

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.

Book a free counselling session

Get a personalized career roadmap

Get tailored program recommendations

Explore industry trends and job opportunities

Programs tailored for your success

Popular

Data Science

Technology

Finance

Management

Future Tech

Upskill with expert articles

View all

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.

Privacy policy and Terms of use

© 2024 Hero Vired. All rights reserved