Python Program to Check Armstrong Number

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

A number of n-digits can be defined as an Armstrong number if and only if the sum of its digits powered to n is equal to the number itself. For example, 1634 is an Armstrong number as

1634=14+64+34+441634 = 1^4 +6^4 +3^4 + 4^4

What is Armstrong Number in Python?

The Armstrong number in Python is the number in which the sum of each digit powered to the total number of digits is the same as the given number. i.e. for a given number say 153,13+53+33153, 1^3 + 5^3 + 3^3 is equal to 153153.

Let's take another example; say 1634=14+64+34+441634 = 1^4 +6^4 +3^4 + 4^4 is an Armstrong number. Any number in any number system that exhibits this sum property will be called an Armstrong number.

Armstrong numbers are also called Narcissistic numbers. They don’t have real-life applications.

what are Armstrong numbers in python

Algorithm to Find a 3-digit Armstrong Number in Python

To find a 3-digit Armstrong number in Python, we start by extracting each digit in the number, multiplying it thrice (to calculate its cube), and then adding all those cubes of digits.

Now, we will compare the sum with the given number. If the sum of cubes is the same as the actual number, then we call it an Armstrong number; otherwise, it is not an Armstrong number.

Step 1: Read the number

Step 2: Declare a variable sum and initialize it to 0

Step 2: For each digit in the number, multiply it thrice and add it to the sum.

Step 3: Check if the sum equals the initial number.

Step 4: If both are equal, then print it as an Armstrong number. Else, print it as not an Armstrong number.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Python Program to Find a 3-digit Armstrong Number

Using While Loop

Code:

Output:

Algorithm to Find an n-digit Armstrong Number in Python

To find if an n-digit number is an Armstrong number or not, we will first calculate the number of digits that is equal to n.

Then we will extract each digit in the number and multiply it n times by itself (to calculate the nth power of it ) and add all those numbers to the sum variable.

Now, we will check if the sum is equal to the given number or not. If both are equal, then we will print it as an Armstrong number or print it as not an Armstrong number.

Step 1: Take the input

Step 2: Calculate the number of digits and store it as digits

Step 3: Declare a variable sum and initialize to 0

Step 4: For each digit in the number, multiply the same digit for digits times and then add it to the sum.

Step 5: Check whether the sum is equal to the input number.

Step 6: Print it as an Armstrong number if it is equal. Else print, not an Armstrong number.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Python Program to Find an n-digit Armstrong Number

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Using While Loop

Code:

Output:

Explanation:

14+64+34+44=16341^4+6^4+3^4+4^4 = 1634

As the sum of digits powered to the number of digits is equal to the number itself, the given number is an Armstrong number.

Using Functions

Code:

Output:

Explanation:

14+64+34+44=16341^4+6^4+3^4+4^4 = 1634

As the sum of digits powered to the number of digits is equal to the number itself, the given number is an Armstrong number.

Using Recursion

Code:

Output:

Explanation:

14+64+34+44=16341^4+6^4+3^4+4^4 = 1634

As the sum of digits powered to the number of digits is equal to the number itself, the given number is an Armstrong number.

Conclusion

  • We learned about what Armstrong numbers are.
  • We also saw how to identify if a number is an Armstrong number in Python or not.
  • We also learned various algorithms and their implementation to check whether the given number is an Armstrong number.

See Also

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more