Armstrong Number Program in Python With Examples & Code Explanation

An Armstrong number is one of the most frequently asked beginner-level Python programming interview questions. It helps learners understand loops, mathematical operations, condition checking, functions, and number manipulation. If you are preparing for Python interviews, coding assessments, or academic exams,...

Armstrong Number in Python

Table of Contents

Phase-Wise Breakdown

Armstrong Number Program in Python With Examples & Code Explanation

An Armstrong number is one of the most frequently asked beginner-level Python programming interview questions. It helps learners understand loops, mathematical operations, condition checking, functions, and number manipulation. If you are preparing for Python interviews, coding assessments, or academic exams, learning Armstrong number logic is extremely important because interviewers often use this problem to test problem-solving ability and programming fundamentals.

According to mentors at Codegnan, number-based programs like Armstrong numbers are excellent for building logical thinking skills and improving debugging ability in beginners.

In this article, we will learn:

  • What an Armstrong number is
  • Armstrong number algorithm
  • Python programs with and without functions
  • Armstrong numbers between 1 to 1000
  • Flowchart explanation
  • Time complexity analysis
  • Common interview questions
  • Practical interview insights from Codegnan trainers

What Is an Armstrong Number?

Armstrong Number

An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the total number of digits.

Formula

For an n-digit number:

abcd…=an+bn+cn+dn+…abcd… = a^n + b^n + c^n + d^n + …

Example 1: 153

153 is a 3-digit number.

13+53+331^3 + 5^3 + 3^3 =1+125+27= 1 + 125 + 27 =153= 153

So, 153 is an Armstrong number.

Example 2: 9474

9474 is a 4-digit number.

94+44+74+449^4 + 4^4 + 7^4 + 4^4 =6561+256+2401+256= 6561 + 256 + 2401 + 256 =9474= 9474

Hence, 9474 is also an Armstrong number.

Armstrong Number Algorithm

Here is the step-by-step algorithm to check whether a number is an Armstrong number.

Algorithm Steps

  1. Start
  2. Input a number
  3. Count the number of digits
  4. Store the original number in a temporary variable
  5. Initialize sum = 0
  6. Extract the last digit using % 10
  7. Raise the digit to the power of total digits
  8. Add the result to sum
  9. Remove the last digit using // 10
  10. Repeat until the number becomes 0
  11. Compare sum with the original number
  12. If both are equal, it is an Armstrong number
  13. Else, it is not an Armstrong number
  14. Stop

Armstrong Number Program in Python

num = int(input("Enter a number: "))

temp = num
digits = len(str(num))
sum = 0

while temp > 0:
    digit = temp % 10
    sum += digit ** digits
    temp //= 10

if sum == num:
    print(num, "is an Armstrong number")
else:
    print(num, "is not an Armstrong number")

Output Example

Example 1

Enter a number: 153
153 is an Armstrong number

Example 2

Enter a number: 125
125 is not an Armstrong number

Code Explanation

Let us understand the code step by step.

1. Taking User Input

num = int(input("Enter a number: "))

This line accepts a number from the user.

2. Counting Digits

digits = len(str(num))

The number is converted into a string to count total digits.

Example:

153  "153"

Length becomes 3.

3. Extracting Digits

digit = temp % 10

This extracts the last digit.

Example:

153 % 10 = 3

4. Removing Last Digit

temp //= 10

Integer division removes the last digit.

Example:

153 // 10 = 15

5. Calculating Power Sum

sum += digit ** digits

Each digit is raised to the power of total digits and added to the sum.

6. Final Comparison

if sum == num:

If the calculated sum equals the original number, it is an Armstrong number.

Codegnan Insights: Common Beginner Mistakes

Common Beginner Mistakes - Codegnan

According to trainers at Codegnan, beginners commonly make these mistakes while solving Armstrong number problems:

  • Forgetting to store the original number
  • Using incorrect power values
  • Missing integer division (//)
  • Not resetting the sum variable
  • Confusing Armstrong numbers with palindrome numbers

Practicing these logic-building programs helps students improve debugging skills and coding confidence.

Armstrong Number Program in Python Using Function

Using functions makes the code cleaner and reusable. This is also the preferred approach in coding interviews.

def armstrong(num):
    digits = len(str(num))
    temp = num
    total = 0

    while temp > 0:
        digit = temp % 10
        total += digit ** digits
        temp //= 10

    return total == num

number = int(input("Enter a number: "))

if armstrong(number):
    print(number, "is an Armstrong number")
else:
    print(number, "is not an Armstrong number")

Why Interviewers Prefer Function-Based Solutions

According to mentors at Codegnan, interviewers prefer function-based implementations because they demonstrate:

  • Modularity
  • Reusability
  • Better code organization
  • Cleaner debugging
  • Professional coding standards

Function-based solutions also make it easier to test multiple inputs.

Armstrong Numbers Between 1 to 1000

Here is a Python program to print Armstrong numbers between 1 and 1000.

for num in range(1, 1001):
    digits = len(str(num))
    temp = num
    total = 0

    while temp > 0:
        digit = temp % 10
        total += digit ** digits
        temp //= 10

    if total == num:
        print(num)

Output

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

Armstrong Number Flowchart

Flowchart Logic

  1. Start
  2. Input number
  3. Count digits
  4. Initialize sum = 0
  5. Extract digit
  6. Calculate power
  7. Add to sum
  8. Remove digit
  9. Repeat until number becomes 0
  10. Compare sum with original number
  11. Display result
  12. Stop

Codegnan Insights: Interview Preparation Tips

Armstrong Prep Tips - Codegnan

According to Python trainers at Codegnan, Armstrong number problems are used in interviews to evaluate logical thinking more than syntax memorization.

Here are some useful interview tips:

1. Explain the Logic Clearly

Interviewers often ask candidates to explain:

  • How digits are extracted
  • Why powers are used
  • Why a temporary variable is needed
  • How the loop works

Good explanation skills improve interview performance significantly.

2. Practice Dry Runs

Before coding, manually trace the program.

Example for 153:

Step Digit Calculation Sum
1 3 33=273^3 = 27 27
2 5 53=1255^3 = 125 152
3 1 13=11^3 = 1 153

Dry runs improve debugging speed during technical rounds.

3. Learn Similar Number Programs

Codegnan trainers recommend practicing:

  • Prime number
  • Palindrome number
  • Perfect number
  • Strong number
  • Fibonacci series

These programs strengthen:

  • Loop concepts
  • Mathematical reasoning
  • Pattern recognition
  • Time complexity understanding

4. Prepare for Follow-Up Questions

After solving the basic problem, interviewers may ask:

  • Can you use functions?
  • Can you print Armstrong numbers in a range?
  • What is the time complexity?
  • Can you optimize the solution?
  • Can you explain every line of code?

Preparing for these variations improves confidence during coding interviews.

Time Complexity Analysis

Time Complexity

O(d)O(d)

Where:

  • d = number of digits

The loop runs once for every digit.

Space Complexity

O(1)O(1)

Only a few variables are used, so memory usage is constant.

Armstrong Number Examples

Number Calculation Armstrong Number?
153 13+53+33=1531^3 + 5^3 + 3^3 = 153 Yes
370 33+73+03=3703^3 + 7^3 + 0^3 = 370 Yes
371 33+73+13=3713^3 + 7^3 + 1^3 = 371 Yes
407 43+03+73=4074^3 + 0^3 + 7^3 = 407 Yes
125 13+23+53=1341^3 + 2^3 + 5^3 = 134 No

Armstrong Number Interview Questions FAQs

What is an Armstrong number in Python?

An Armstrong number is a number equal to the sum of its digits raised to the power of the total number of digits.

Example:

153=13+53+33153 = 1^3 + 5^3 + 3^3

Is 1634 an Armstrong number?

Yes.

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

So, 1634 is an Armstrong number.

What is the time complexity of the Armstrong number program?

The time complexity is:

O(d)O(d)

Where d is the number of digits.

Why are Armstrong number programs asked in interviews?

Interviewers use Armstrong number problems to test:

  • Loop understanding
  • Mathematical logic
  • Number manipulation
  • Function usage
  • Problem-solving ability

Can Armstrong numbers be negative?

No. Armstrong numbers are generally defined only for non-negative integers.

What are the Armstrong numbers from 1 to 1000?

The Armstrong numbers between 1 and 1000 are:

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

Why should beginners practice Armstrong number programs?

According to trainers at Codegnan, Armstrong number programs help beginners improve:

  • Logical thinking
  • Loop understanding
  • Debugging skills
  • Mathematical problem solving
  • Confidence in coding interviews

Key Takeaway

Armstrong number programs are excellent for learning Python fundamentals such as loops, conditions, functions, operators, and mathematical logic. They are also among the most frequently asked beginner-level coding interview questions. By practicing Armstrong number problems using different approaches, students can improve logical thinking, debugging ability, and coding confidence. Training platforms like Codegnan often include these logic-building exercises in Python and interview preparation programs because they help students develop strong programming fundamentals early in their learning journey.

Leave a Reply

Your email address will not be published. Required fields are marked *

Similar Topics

Explore the Java developer salary in India for 2026, right from fresher pay of Rs 3.5 LPA to senior architect packages crossing Rs 55 LPA. Get city-wise, experience-wise, and role-wise...

Categories

At Codegnan, we have trained 4,000+ students to become Java developers with placement assistance. So, with our experience and training of Java students for 6+ years, we know the ins...

Categories

Discover the best programming languages to learn in India in 2026 for jobs, salary, AI, web development, and software engineering careers....

Categories

Chat with us WhatsApp

Choose your
Comfortable place

Complete the form to secure your spot. Our team will contact you with course details, orientation steps, and next actions.

Register & Start Your Learning Journey

Complete the form to secure your spot. Our team will contact you with course details, orientation steps, and next actions.