Close Menu

    Maximum Consecutive Ones | Leetcode 485 | Explained with Images

    March 24, 2025

    Find missing number in an array | Leetcode 268 | Explained

    March 14, 2025

    Union of 2 Sorted Arrays with Duplicates | Explained with Images

    March 13, 2025

    Linear Search in Python | Explained with Images and Examples

    March 10, 2025
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram Pinterest Vimeo
    Code and Debug AcademyCode and Debug Academy
    Subscribe
    Code and Debug AcademyCode and Debug Academy
    Home»Training»Python Program to Check Armstrong Number | Explained
    Training

    Python Program to Check Armstrong Number | Explained

    Code and DebugBy Code and DebugDecember 30, 2024No Comments4 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Python Program to Check Armstrong Number
    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.

    Contents
     [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: 13 + 53 + 33 = 1 + 125 + 27 = 153
    
    Example 2:
    
      Input: N = 371
      Output: True
      Explanation: 33 + 53 + 13 = 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.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Code and Debug
    • Website

    Related Posts

    Training

    Best Data Analytics Training in Kochi for Clearing Placements

    February 26, 2025
    Training

    Best Data Analytics Course in Noida using Python

    February 25, 2025
    Training

    Best Data Analytics Training in Surat for Placement Interviews

    February 18, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Facebook X (Twitter) Instagram Pinterest
    © 2025 ThemeSphere. Designed by ThemeSphere.

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