Sai A
Updated date Nov 01, 2023
In this blog, we will explore Armstrong numbers, also known as narcissistic numbers, and provide a Python program to check for them.

## Introduction:

Numbers have always fascinated mathematicians, and some numbers are more intriguing than others. One such group of numbers is the Armstrong numbers, also known as narcissistic numbers. These numbers exhibit a unique property that has piqued the interest of many. In this blog, we will learn the world of Armstrong numbers, explain their characteristics, and provide two methods to check if a given number is an Armstrong number.

## What is an Armstrong Number?

An Armstrong number (or narcissistic number) is a special type of number in mathematics. It is a number that is equal to the sum of its own digits, each raised to the power of the number of digits. To put it simply, an n-digit number is an Armstrong number if the sum of its digits, each raised to the nth power, equals the number itself.

For example, let's take the number 153:

• It has 3 digits, so n = 3.
• The sum of its digits raised to the power of 3 is 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.

As you can see, 153 satisfies the Armstrong number condition and is, therefore, an Armstrong number.

Now, let's write a Python program to check whether a given number is an Armstrong number or not.

## Method 1: Using a Loop

``````def is_armstrong(number):
# Count the number of digits
num_str = str(number)
n = len(num_str)

# Calculate the sum of digits raised to the nth power
total = sum(int(digit) ** n for digit in num_str)

# Check if the total is equal to the original number

# Input from the user
num = int(input("Enter a number: "))

# 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: 153
153 is an Armstrong number.
``````

In this method, we take the input number, count the number of digits, and then calculate the sum of each digit raised to the nth power. Finally, we check if the total is equal to the original number.

## Method 2: Using Recursion

In this method, we'll create a recursive function to check for Armstrong numbers.

``````def is_armstrong_recursive(number, n, total=0):
if number == 0:

digit = number % 10
total += digit ** n
return is_armstrong_recursive(number // 10, n, total)

# Input from the user
num = int(input("Enter a number: "))
num_str = str(num)
n = len(num_str)

# Check if it's an Armstrong number
if is_armstrong_recursive(num, n):
print(num, "is an Armstrong number.")
else:
print(num, "is not an Armstrong number.")
``````

### Output:

``````Enter a number: 370
370 is an Armstrong number.
``````

In this recursive approach, we check each digit of the number, starting from the rightmost digit, and accumulate their nth power. If the total is equal to the number, it's an Armstrong number.

## Method 3: Using a Generator

In this method, we'll create a generator function to generate Armstrong numbers. This can be useful if you want to find Armstrong numbers within a certain range.

``````def armstrong_generator():
for num in range(1, 10**6):  # Change the range as needed
num_str = str(num)
n = len(num_str)
total = sum(int(digit) ** n for digit in num_str)
if total == num:
yield num

# Print Armstrong numbers from 1 to 10^6
for armstrong in armstrong_generator():
print(armstrong, end=", ")
``````

### Output:

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

This method generates Armstrong numbers within a specified range.

## Conclusion:

Armstrong numbers are a unique and intriguing mathematical concept. They are numbers that exhibit a fascinating property - the sum of their digits, each raised to the power of the number of digits, equals the number itself. In this blog, we have provided two methods for checking whether a number is an Armstrong number using Python: one using a loop and another using recursion. We've also shown how to generate Armstrong numbers using a generator.

There are no comments. Be the first to comment!!!