Armstrong Numbers in Python with Examples

4 minute read
10 shares
Armstrong numbers in python

If you’re new to the world of computer programming and Python, you might have heard the term “Armstrong Number”. When each positive integer is raised to the power of n and the sum is equal to the original number or in other ways, a number is equal to the sum of the cubes of its own digit it is called the armstrong number. Don’t worry; we’re here to simplify it for you! In this blog, we’ll explain what are Armstrong Numbers in Python. We’ll use simple language and provide clear examples along the way so stay tuned and continue reading!

Also Read: Know all about Computer Programming

What is an Armstrong Number?

An Armstrong Number is a number that is equal to the sum of its own digits, each raised to the power of the number of digits in the number. In other words, it’s a number for which the sum of its own digits, each raised to a power, results in the number itself.

Let’s break this down into a few key points:

  • An Armstrong Number has n digits, where n is the number of digits in the number.
  • Each digit in the number is raised to the power of n.
  • The results of these power operations are summed up.
  • If the sum is equal to the original number, it’s an Armstrong Number.

Also Read – How to Become a Python Developer in India: Salary and Perks

Examples of Armstrong Numbers

Here are a few examples of the numbers:

  • 371

– Number of digits: 3

– Calculation: 3*3 + 7*3 + 1*3 = 27 + 343 + 1 = 371

– Explanation: 371 is an Armstrong number because the sum of its digits, each raised to the power of 3, equals 371.

  • 407

– Number of digits: 3

– Calculation: 4*3 + 0*3 + 7*3 = 64 + 0 + 343 = 407

– Explanation: 407 is an Armstrong number because the sum of its digits, each raised to the power of 3, equals 407.

  • 1634

– Number of digits: 4

– Calculation: 1*4 + 6*4 + 3*4 + 4*4 = 1 + 1296 + 81 + 256 = 1634

 – Explanation: 1634 is an Armstrong number because the sum of its digits, each raised to the power of 4, equals 1634.

  • 9474

 – Number of digits: 4

 – Calculation: 9*4 + 4*4 + 7*4 + 4*4 = 6561 + 256 + 2401 + 256 = 9474

  – Explanation: 9474 is an Armstrong number because the sum of its digits, each raised to the power of 4, equals 9474.

Also Read – A Complete Guide to Full-Stack Development 

How to Check for Armstrong Number in Python?

Now that we understand the concept, let’s dive into how to find Armstrong Numbers using Python. We’ll provide a straightforward Python code example for this.

python

def is_armstrong(number):

Convert the number to a string to count its digits

num_str = str(number)

Get the number of digits in the number

num_digits = len(num_str)

Initialize a variable to store the sum of digit powers

armstrong_sum = 0

Calculate the sum of digit powers

for digit in num_str:

armstrong_sum += int(digit) ** num_digits

Check if the sum is equal to the original number

return armstrong_sum == number

Example of usage:

num = 370

if is_armstrong(num):

    print(f”{num} is an Armstrong Number.”)

else:

    print(f”{num} is not an Armstrong Number.”)

In this code:

  • Convert the number to a string to count its digits.
  • Calculate the number of digits in the number.
  • Initialize a variable to store the sum of digit powers.
  • Rewrite each digit, raise it to the power of the number of digits, and add it to the sum.
  • Finally, check if the sum is equal to the original number.
Source – The Programming Portal 

How to check the function?

Now, you can use this `is_armstrong_number` function to check if a number is an Armstrong number. Here are some examples:

Python

print(is_armstrong_number(153))   # Output: True

print(is_armstrong_number(371))   # Output: True

print(is_armstrong_number(407))   # Output: True

print(is_armstrong_number(1634))  # Output: True

print(is_armstrong_number(9474))  # Output: True

print(is_armstrong_number(123))   # Output: False (Not an Armstrong number)

Simply call the `is_armstrong_number` function with the number you want to check, and it will return `True` if the number is an Armstrong number or `False` otherwise.

Armstrong Numbers are a fascinating concept in mathematics and programming. With Python, you can easily check if a given number is an Armstrong Number or not. We hope this blog has helped you understand Armstrong Numbers in Python. Happy coding!

Relevant Blogs

What are Human Rights and Why are they important?Engineer’s Day 2023: Theme
Engineer’s Day Celebration IdeasNational Techies Day 2023
Top 10 Funny Engineer’s Day WishesNational Engineer’s Day 2023: 10 Quotes to Relate With!
National Thermal Engineer Day 2023National Engineer’s Day: Memes

FAQS

What is 2 digit Armstrong number?

There are no two-digit Armstrong numbers. These numbers start with three digits and are 153,370,371,407, etc.

Why is Armstrong number used?

Armstrong numbers are used to strengthen the 9-digit algorithm for encrypting and decrypting data. If necessary, the length of an Armstrong number can be increased to enhance security.

Can a negative number be Armstrong number?

An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits. However, the concept of an Armstrong number is only applicable to positive integers. It is not possible to check for Armstrong numbers in negative numbers or decimal numbers since they are not considered Armstrong numbers. Therefore, when checking for Armstrong numbers, it is important to only consider positive integers.

That was all about Armstrong Numbers in Python. For more information about such informative articles, check the trending events page of Leverage Edu.

Leave a Reply

Required fields are marked *

*

*