Close Menu
    Code and Debug
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook X (Twitter) Instagram YouTube WhatsApp
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook Instagram YouTube LinkedIn WhatsApp
    Code and Debug – BlogCode and Debug – Blog
    Code and Debug – BlogCode and Debug – Blog
    Home»Data Structures & Algorithms»Python Program to Check Armstrong Number | Explained
    Data Structures & Algorithms

    Python Program to Check Armstrong Number | Explained

    codeanddebugBy codeanddebug28 May 2025Updated:4 April 2026No Comments5 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Featured Image of a question Check if a number is Armstrong number on leetcode
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Hello curious minds! In this article we’ll explore the Python program to check Armstrong Number. Explained comprehensively with examples, code, edge cases, dry run, time and space complexity.

    Here is the [Problem Link] for your quick reference.

    Content
     [show]
    • Examples of Python Program to Check Armstrong Number:
    • Solution
      • Problem Statement
      • Intuition and Approach
      • Code
      • Dry Run
      • Potential Edge Cases:
      • Handling Edge Cases
      • Time and Space Complexity

    Examples of Python Program to Check Armstrong Number:

    Example 1:
    
      Input: N = 153
      Output: True
      Explanation: 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
    
    Example 2:
    
      Input: N = 371
      Output: True
      Explanation: 3^3 + 5^3 + 1^3 = 27 + 343 + 1 = 371

    Understand by watching video


    Solution

    Problem Statement

    Objective: We aim to determine whether a given integer “num” is an Armstrong number (also known as a narcissistic number). An Armstrong number is one that is equal to the sum of its own digits each raised to the power of the number of digits.

    Purpose: The function “isArmstrong” checks if the integer “num” is an Armstrong number by calculating the sum of its digits each raised to the power of the number of digits and comparing it to the original number.

    Expected Input and Output:

    • Input: A positive integer “num”.
    • Output: A boolean value True if “num” is an Armstrong number, and False otherwise.

    Also read Palindrome Number Program in Python | Leetcode #9

    Intuition and Approach

    Intuition: To determine if a number is an Armstrong number, we can break the number into its individual digits, raise each digit to the power of the total number of digits, and sum these values. If the sum equals the original number, then it is an Armstrong number.

    Approach:

    1. Convert the number to a string to determine the number of digits
    2. Initialize a variable to store the sum of the digits each raised to the power of the number of digits
    3. Use a loop to extract each digit, raise it to the appropriate power, and add the result to the sum
    4. Compare the computed sum to the original number to determine if it is an Armstrong number

    Code

    def isArmstrong(num):
        k = len(str(num))
        arm_sum = 0
        n = num
        while n > 0:
            ld = n % 10
            arm_sum += ld**k
            n = n // 10
        return arm_sum == num
    1. Number of Digits: The variable “k” is set to the length of the string representation of “num”, giving the number of digits in “num”.
    2. Initialize Sum: The variable “arm_sum” is initialized to 0 to accumulate the sum of the digits raised to the power of “k”.
    3. Loop to Calculate Sum:
      1. While “n” is greater than 0, the last digit “ld” is extracted using n % 10
      2. This digit is raised to the power of “k” and added to “arm_sum”
      3. The last digit is removed from “n” using integer division by 10
    4. Return Comparison: The function returns True if “arm_sum” is equal to “num”, and False otherwise.

    Dry Run

    Let’s walk through a step-by-step execution with a sample image given below, this example demonstrates when a number is not an Armstrong number.

    Dry Run of the Python Program to Check Armstrong Number - 1

    Dry Run of the Python Program to Check Armstrong Number - 2

    Potential Edge Cases:

    1. Single-Digit Numbers:
      • Any single-digit number should return True as any number is equal to itself raised to the power of one (e.g., num = 5).
    2. Zero:
      • num = 0 should return True as 0^1 = 0
    3. Large Numbers:
      • The function should handle large numbers correctly and check if they are Armstrong numbers.

    Handling Edge Cases

    • The current code handles single-digit numbers and zero correctly.
    • It also works for large numbers due to Python’s ability to handle large integers.

    Time and Space Complexity

    Time Complexity: “O(log10N + 1)” where “N” is the input number. The time complexity is determined by the number of digits in the input integer “N”. In the worst case when “N” is a multiple of 10 the number of digits in N is “log10 N + 1”.

    • In the while loop we divide N by 10 until it becomes 0 which takes “log10N” iterations.
    • In each iteration of the while loop we perform constant time operations like modulus and division and pushing elements into the vector.

    Space Complexity: “O(1)” as only a constant amount of additional memory for the reversed number regardless of size of the input number.


    Some Questions for You

    What happens if the number ends with trailing zeros (e.g., 1200)?

    Trailing zeros are naturally removed during reversal because integer operations ignore leading zeros in the result. For example, reversing 1200 gives 21, not 0021. This is expected behavior and does not require special handling.

    Can integer overflow occur while reversing the number?

    Yes, in languages like C++ or Java where integers have fixed limits. While building the reversed number, multiplying by 10 can exceed the integer range. In such cases, you must check for overflow before updating the result.

    How does the algorithm behave for negative numbers?

    The logic remains the same, but you typically store the sign separately. Reverse the absolute value and then reapply the sign at the end. For example, -123 becomes -321.

    Is there any faster approach than repeatedly dividing by 10?

    No, because any method must process each digit at least once. Whether you use arithmetic operations or convert to a string, the lower bound remains proportional to the number of digits, i.e., O(log₁₀N).

    Why don’t we use extra data structures like arrays or stacks here?

    Because the problem can be solved optimally using pure arithmetic. Using extra data structures would increase space complexity to O(N) without improving time complexity, making the solution less efficient.

    Watch our python dsa course on youtube (200+ questions)

    For any changes to the document, kindly email at code@codeanddebug.in or contact us at +91-9712928220.

    Easy Math
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticlePalindrome Number Program in Python | Leetcode #9
    Next Article Python Program to Print Divisors/Factors of an Integer
    codeanddebug
    • Website

    Related Posts

    Data Structures & Algorithms

    DSA Interview Cheat Sheet: All Patterns You Need to Know

    4 April 2026
    Data Structures & Algorithms

    Symmetric Tree | Leetcode 101 | Recursive DFS Approach

    2 September 2025
    Data Structures & Algorithms

    Binary Tree Right Side View | Leetcode 199 | BFS and DFS Approaches

    2 September 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Categories
    • Data Structures & Algorithms (241)
      • Beginner (82)
      • Expert (52)
      • Intermediate (106)
    • Pandas (1)
    • Resume (1)
    Recent Posts

    Pandas Python: The Complete Roadmap for Beginners in 2026

    8 April 2026

    DSA Interview Cheat Sheet: All Patterns You Need to Know

    4 April 2026

    Symmetric Tree | Leetcode 101 | Recursive DFS Approach

    2 September 2025

    Binary Tree Right Side View | Leetcode 199 | BFS and DFS Approaches

    2 September 2025

    Bottom View of Binary Tree | BFS with Horizontal Distance Mapping

    2 September 2025
    Facebook Instagram YouTube LinkedIn WhatsApp
    © 2025 Code and Debug. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.