In Python, how can we determine whether an n-digit integer is an Armstrong number or not?

Associate
Joined
20 Jan 2023
Posts
5
Location
India
So, after reading and studying, I attempted to answer a question. Assume a user enters an n-digit number. How do we know if it's Armstrong or not? One technique may be to record a list of all Armstrong numbers and then check from that list, but I wanted to use a different approach. This is my code...

Python:
#armstrong number
take_in=int(input("Enter the number: "))
num2=take_in
length=len(str(take_in))
rep=length
summ=0
while rep>0:
    summ=summ+(num2/10**rep)**length
    num2=num2%(10**rep)
    rep=rep-1
    if rep==0:
        if summ==take_in:
            print("{} is an armstrong number".format(take_in))
        else:
            print("{} is not an armstrong number".format(take_in))
 
Back
Top Bottom