Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Jul 9, 2024
Difficulty: Medium

Python Program to Check Armstrong Number

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

An Armstrong number (or narcissistic number) is a number that equals the sum of its own digits each raised to the power of the number of digits. In Python, you can check if a number is an Armstrong number by adding the powered digits and comparing the result to the original number.

Armstrong Number In Python

What is Armstrong Number in Python?

Armstrong numbers are special numbers in mathematics which are having special properties. A number K of length (number of digits) n is said to be Armstrong if and only if the sum of its digits raised to be power n is equal to K. 

Let's Discuss some examples:

  1. Number = 407
    This number is an Armstrong number because
    4*4*4+ 0*0*0 + 7*7*7 = 407. Which is equal to the given number.
     
  2. Number = 121
    This number is not an Armstrong number because
    1*1*1 + 2*2*2 + 1*1*1 = 9. Which is not equal to 121, hence not an Armstrong number.
     
  3. Number = 8208
    This number is an Armstrong number because,
    8*8*8*8 + 2*2*2*2 + 0*0*0*0 + 8*8*8*8 = 8203. Which is equal to the given number.
     
  4. Number = 1010
    This number is not an Armstrong number because
    1*1*1*1 + 0*0*0*0 + 1*1*1*1 +0*0*0*0 = 2. Which is not equal to 1010, hence not an Armstrong number.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

How to check Armstrong Numbers In Python?

Method 1: Using String Manipulation

Algorithm:

Step 1: Convert the given number into a string 
Step 2: Find the string length.
Step 3: Initialize a variable didit_sum to 0.
Step 4:

  • Iterate over every digit of the string.
  • Convert the digit to an integer.
  • Find the digit raised to the power of the length of num.
  • Add it to digit_sum.
     

Step 5: If the digit_sum is equal to the given number, return true else, false. 

Method 2: Find the Sum Digit by Digit.

In this method, Instead of converting the given number to the string, we directly apply the method over the number.

Algorithm

For a number 'K' has 'n' number of digits, an algorithm can be written as 

Step 1: Count the number of digits in the given number.

Step 2: Calculate the sum of digits raised to the power n(number of digits). We will use a while loop to iterate over every digit of the number.

Step 3: Compare the resultant sum to the original number. If the resultant sum is equal to the input number, then a number is Armstrong; else not. 

Python Program to Find a 3 Digit Armstrong Number

  • Python

Python

def armstrong(n):
sum = 0
temp = n
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
return sum == n

for i in range(100, 1000):
if armstrong(i):
print(i)

Output

output

In the above code, we have created an Armstrong function which gives us the Armstrong number. We have asked the Armstrong function to give the Armstrong number between 100 to 1000. And for doing so, we have calculated the sum of the cube of each digit. 

So, we have initialized the sum to 0 and obtained each digit by using the % operator. The remainder of a number, when divided by 10, gives us the last digit of that number. Then we took the cubes using the exponent operator. 

Python Program to Find an N Digit Armstrong Number

  • Python

Python

def is_armstrong_number(number, n):
# Convert the number to a string to count its digits
num_str = str(number)
num_digits = len(num_str)

# Calculate the sum of each digit raised to the power of n
digit_sum = sum(int(digit) ** n for digit in num_str)

# Check if it's an Armstrong number
return number == digit_sum

def find_n_digit_armstrong_numbers(n):
lower_limit = 10**(n - 1) # Smallest N-digit number
upper_limit = 10**n - 1 # Largest N-digit number

armstrong_numbers = [number for number in range(lower_limit, upper_limit + 1) if is_armstrong_number(number, n)]

return armstrong_numbers

# Input the value of N
n = int(input("Enter the value of N: "))

# Find and print N-digit Armstrong numbers
armstrong_numbers = find_n_digit_armstrong_numbers(n)
print(f"{n}-digit Armstrong numbers: {armstrong_numbers}")

Output

output

The above program first takes the user input and then gets the smallest and largest Armstrong number, then it checks whether it is an Armstrong number or not; if yes, then it returns us the Armstrong number. In the given example, we have taken 4 as the input so it has returned us the three values of Armstrong number with digit 4.

Note: We have to give the custom input. Here the custom input given is 4.

Frequently Asked Questions

How do you check whether the given number is an Armstrong number in Python?

In Python, we write a program and initialize the sum to 0 and find the sum of digits raised to the power of the length of the number, then compare the resultant sum to the original number. If both are equal, the number is Armstrong; otherwise, not.

Can a decimal number be an Armstrong number?

No, a decimal number can never be an Armstrong number because the power of the decimal numbers is not well defined. So Armstrong numbers should be positive integers.

Is 371 an Armstrong number?

Yes, 371 is an Armstrong number because the sum of the cubes of its digits (3^3 + 7^3 + 1^3) equals 371, which is the same as the original number.

Why is 1634 an Armstrong number?

1634 is an Armstrong number because the sum of its digits each raised to the power of four (the number of digits) equals the number itself.

Conclusion

In conclusion, a Python program to check Armstrong numbers involves adding the digits of a number, each raised to the power of the number of digits, and comparing this sum to the original number. If they match, the number is an Armstrong number, demonstrating an interesting property of certain numbers.

We recommend you to read these articles related to this article:

  1. Python Program to Print the Fibonacci Sequence
     
  2. Floor Division in Python
     
  3. Max Heap in Python
     
  4. fibonacci series in c
     
  5. Introduction to Javascript
     

We hope you enjoyed the article. Remember to upvote if you liked it.

Happy Learning!!

Topics covered
1.
Introduction
2.
What is Armstrong Number in Python?
3.
How to check Armstrong Numbers In Python?
3.1.
Method 1: Using String Manipulation
3.2.
Method 2: Find the Sum Digit by Digit.
4.
Python Program to Find a 3 Digit Armstrong Number
4.1.
Python
5.
Python Program to Find an N Digit Armstrong Number
5.1.
Python
6.
Frequently Asked Questions
6.1.
How do you check whether the given number is an Armstrong number in Python?
6.2.
Can a decimal number be an Armstrong number?
6.3.
Is 371 an Armstrong number?
6.4.
Why is 1634 an Armstrong number?
7.
Conclusion