Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Computer Fundamentals»Python Program to Check Armstrong Number

    Python Program to Check Armstrong Number

    By RahulJune 16, 20233 Mins Read

    An Armstrong number is a number that is equal to the sum of its digits each raised to the power of the number of digits. For instance, in a 3-digit number like 153, the sum of the cubes of each digit (1^3 + 5^3 + 3^3 = 153) equals the number itself. Thus, 153 is an Armstrong number.

    In this article, we will explore a Python program to check whether a number is an Armstrong number. This is a common problem, often seen in computer programming and coding interviews.

    Prerequisites

    To understand and implement this program, you should have a basic understanding of Python programming. Specifically, you should be comfortable with:

    1. Variables and data types
    2. Control structures (for loops, if statements)
    3. Functions
    4. User input/output

    Program Description

    Our Python program will perform the following steps:

    1. Ask the user for a number (input).
    2. Determine the number of digits in the given number.
    3. Compute the sum of each digit to the power of the number of digits.
    4. Compare the calculated sum with the original number.
    5. If they are equal, print that the number is an Armstrong number.
    6. Otherwise, print that the number is not an Armstrong number.

    Code Implementation

    Here is the Python code that accomplishes this task:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    # Function to check if a number is an Armstrong number
    def is_armstrong(num):
        # Convert the number to string to calculate the length (number of digits)
        num_str = str(num)
        num_len = len(num_str)
     
        # Calculate the sum of each digit to the power of the number of digits
        sum = 0
        for digit in num_str:
            sum += int(digit) ** num_len
     
        # The number is an Armstrong number if the sum equals the original number
        return sum == num
     
     
    # Get input from the user
    num = int(input("Enter a number: "))
     
    # Check if the number is an Armstrong number and print the result
    if is_armstrong(num):
        print(f"{num} is an Armstrong number.")
    else:
        print(f"{num} is not an Armstrong number.")

    Code Explanation

    Here’s how the code works:

    1. The function is_armstrong takes a number as an argument. It converts the number into a string with str(num) to determine the number of digits using the len function.
    2. It then initializes a variable sum to 0. This variable will hold the sum of each digit to the power of the number of digits.
    3. The for loop iterates over each digit in the number. It adds the digit (converted back to an integer) raised to the power of the number of digits to sum.
    4. The function returns True if sum equals the original number (meaning the number is an Armstrong number) and False otherwise.
    5. The main part of the program asks the user for a number with input. It converts the input to an integer because input returns a string.
    6. It then calls is_armstrong with the user’s number. If the function returns True, it prints that the number is an Armstrong number. Otherwise, it prints that the number is not an Armstrong number.

    Examples

    Let’s look at a couple of examples to understand the program better:

    Example 1:

    1
    2
    Enter a number: 153
    153 is an Armstrong number.

    Explanation: The number 153 has 3 digits. The sum of each digit to the power of 3 is 1^3 + 5^3 + 3^3 = 153, which is equal to the original number.

    Example 2:

    1
    2
    Enter a number: 123
    123 is not an Armstrong number.

    Explanation: The number 123 also has 3 digits. However, the sum of each digit to the power of 3 is 1^3 + 2^3 + 3^3 = 36, which is not equal to the original number.

    Conclusion

    In this article, we’ve learned how to create a Python program to check if a number is an Armstrong number. We’ve explored the definition of Armstrong numbers, the prerequisites for understanding the program, and the code implementation with explanations. Finally, we walked through some examples to illustrate how the program works. With this knowledge, you can solve similar problems involving Armstrong numbers and enhance your Python coding skills.

    Armstrong Number
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    What is a Transparent Proxy Server?

    What is a Master-Worker Model?

    What is an Orchestration System?

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Using .env Files in Django
    • Using .env File in FastAPI
    • Setting Up Email Notifications for Django Error Reporting
    • How to Enable Apache Rewrite (mod_rewrite) Module
    • What are Microservices?
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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