Python Program to Check Armstrong Number | Armstrong Number in Python

Posted in

Python Program to Check Armstrong Number | Armstrong Number in Python
vinaykhatri

Vinay Khatri
Last updated on November 23, 2024

    In this python program, we will ask the user to enter a number and as an output, the program will tell if the entered number is an Armstrong number or not.

    What is Armstrong Number?

    A number is said to be Armstrong's number if the sum of that number each digit which is raised to the power of total digits present in that number is equal to the number itself. for instance: 153 is an Armstrong number because: 153 = (1) 3 + (5) 3 +(3) 3

    Prerequisite Topics Armstrong Number in Python

    • Python Input, Output
    • Python Data types
    • Python Type Conversion
    • Python loop
    • Python Arithmetic and comparisons Operators
    Steps
    • First, we will ask the user to enter a number
    • now using the len() function we compute the total digit present in the entered number.
    • now using the for loop we will go through each digit present in the number and raise it to the power of the total length of the number, and simultaneously we will also compute its sum.

    Armstrong Number in Python Program

    n = input("Enter a number: ")    # n is a string not a number
    
    power = len(n)
    sum=0
    
    #loop through a string
    for i in n:
        sum += int(i)**power
    
    if sum == int(n):
        print(n + " is an Armstrong Number")
    else:
        print(n+" is not an Armstrong Number")

    Output 1:

    Enter a number: 1634
    1634 is an Armstrong Number

    Output 2:

    Enter a number: 23
    23 is not an Armstrong Number

    People are also reading:

    Leave a Comment on this Post

    0 Comments