'Armstrong numbers in python

num=int(input("please enter number: "))
for num in range(num, 1000):
    sum1=0
    numcp=num
    if(num>=10 and num<100):
        while(num>0):
            digit=int(num%10)
            d2=digit*digit
            sum1=sum1+d2
            num=int(num/10)

    if(num>=100 and num<1000):
        while(num>0):
            digit=int(num%10)
            d2=digit*digit*digit
            sum1=sum1+d2
            num=int(num/10)
    if(numcp==sum1):
        print("angstrong number: ", sum1)

what is wrong with this python code. it works till 407 then stops. i am unable to figure out why it stops when the range i have provided is 1000.

Edited

any other more pythonic way of doing this for number till 100000 or 1000000.

i don’t want to copy this if block ( if(num>=10 and num<100) ) many times which decides the number of digits.



Solution 1:[1]

Armstrong numbers? You seem to be right so far

The following is the output from the above program. Thus, there are six Armstrong numbers in the range of 0 and 999.
Armstrong numbers: 0, 1, 153, 370, 371, 407

If you want to know if you reached the end you should probably put a

print(num)

after the loop

Solution 2:[2]

Here's a new way

n=list(map(int, raw_input()))

l=len(n)
sum=0
for i in n:
    sum=sum+pow(i,l)

sum=str(sum)
s=[]
for y in sum:
    s.append(int(y))

if(sorted(n)==sorted(s)):
    print('armstrong')
else:
    print('not armstrong')

Solution 3:[3]

It's simple and readable to iterate over each character in a String with Python. If you convert the input number to a String it will make it simple to iterate over the number digits and calculate whether a number is an Armstrong Number

Check out this straightforward solution:

def armstrong_number(number):
    result = 0
    snumber = str(number)
    l = len(snumber)
    for digit in snumber:
        result += int(digit)**l
        if result > number:
            return False
    if result != number:
        return False
    return True

Solution 4:[4]

Here's mine

a = input("Please enter a number: ")
t = 0
for i in a: t += int(i) ** len(a)
print(a, "Armstrong.") if t == int(a) else print(a, "Not Armstrong.")

Solution 5:[5]

###### List Comprehension to find Armstrong Number ######

num = raw_input('Enter the number: ')

if sum([int(i) ** len(num) for i in num]) == int(num):
    print num, 'is a Armstrong number'
else:
    print num, 'is NOT Armstrong number'

Solution 6:[6]

This is Simple Armstrong Number Program in Python

n=input("Enter")
a=len(n)
s=0
x=0
c=int(n)
for i in range(0,a):
    x=int(n[i])
    s=s+x**a
if s==c:
    print("amstrong")
else:
    print("no")

Solution 7:[7]

Armstrong Number Finder(Easy version)

lst = []
number = int(input("Enter number: "))
sum1 = 0
a = number

while number>0:
    lst.append(number%10)
    number = number//10

for i in range(0,len(lst)):
    lst[i]=lst[i]**3
    sum1+=lst[i]

print(sum1)

if(sum1 == a):
    print("This is an armstrong number")
else:
    print("This is not an armstrong number")

Solution 8:[8]

The simplest way to do so is by list comprehension. The following snippet generates the Armstrong numbers using list comprehension between the range 0 to 99999

print("armstrong a.k.a. narcissistic numbers : ")
armstrong = [x for x in range(100000) if sum(list(map(lambda y: y ** len(str(x)), list(map(int, str(x)))))) == x]
print(armstrong)

Output :

armstrong a.k.a. narcissistic numbers :

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084]

Solution 9:[9]

Or more golf code way:

n = input("Please enter a number: ")
print(f"{n} is {'' if int(n) == sum(int(i)**len(n) for i in n) else 'not '}an Armstrong number")

Output for example n = 54748 and 1000:

# 54748 is an Armstrong number
# 1000 is not an Armstrong number

Solution 10:[10]

Armstrong Function- Basic one

def Armstrong(num):
    listt = []
    summ = 0
    for i in str(num):
        carry = pow(int(i),len(str(num)))
        listt.append(carry)
    for i in listt:
        summ = summ+i
    if num == summ:
        print(num , "is an Armstrong number")
    else:
        print(num , "is NOT an Armstrong number")

Solution 11:[11]

This is one way to solve it.

def getamstrong(n):
    val = [int(i) for i in str(n)]
    new = []
    for i in val:
        new.append(i**len(val))
    if sum(new) == n:
        print(f"{n} is an amstrong number")
    else:
        print(f"{n} is not an amstrong number")
        
n = 1634
getamstrong(n)

Solution 12:[12]

# The Below Function can display one number as an armstrong,
# or you could simply pass a range in the num_list to find all armstrong numbers in the 
# list.

def armStrong(num):
    number = str(num)
    digit = len(number)
    check = sum([int(i)**digit for i in number])
    if check==num:
        return "Armstrong"
    else:
        return "Not Armstrong"
print(armStrong(153))
num_list = [i for i in range(99800,100000)]
for i in num_list:
    print(f"{i} = {armStrong(i)}")

Solution 13:[13]

enter image description here#armstrong Numbers

Generate numbers between the range you want to check for existance of armstrong numbers

for x in range(100,1000):
    #Get the digits from the number generated as a, b, c
    a = x//100
    b = (x%100)//10
    c = x%10
    #now check whether the number is armstrong number or not
    #here, a**3 + b**3 + c**3 == abc
    if((a**3) + (b**3) + (c**3) == x):
        print("Armstrong Number {}".format(x))