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:28 May 2025No Comments4 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

    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.


    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

    Bubble Sort Algorithm in Python | Explained in Depth

    29 May 2025
    Data Structures & Algorithms

    Python Program to Find Factorial of Number Using Recursion

    29 May 2025
    Data Structures & Algorithms

    Python Program to Print from 1 to N Without Loops

    29 May 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Categories
    • Data Structures & Algorithms (30)
      • Beginner (16)
      • Expert (5)
      • Intermediate (9)
    • Uncategorised (1)
    Recent Posts

    Bubble Sort Algorithm in Python | Explained in Depth

    29 May 2025

    Python Program to Find Factorial of Number Using Recursion

    29 May 2025

    Python Program to Print from 1 to N Without Loops

    29 May 2025

    Python Program to Print Divisors/Factors of an Integer

    28 May 2025

    Python Program to Check Armstrong Number | Explained

    28 May 2025
    Facebook Instagram YouTube LinkedIn WhatsApp
    © 2025 Code and Debug. All rights reserved.

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