Show all Armstrong number up-to 1000 in python by R4R Team

Armstrong number -

A number is said to be an armstrong number if it's sum of digit with power of length of number is equal to the actual number.

example -
153 is an armstrong number because 1**3+5**3+3**3=153

Program to show the all armstrong number from 0 to 1000-

def checkarmstrong(n):
ans=0
a=n
l=len(str(n))
while(a>0):
t=a%10
ans=ans+t**l
a=int(a/10)
if ans==n:
return 1
else:
return 0

for i in range(1000):
if checkarmstrong(i):
print(i,end=" ")


output -

0 1 2 3 4 5 6 7 8 9 153 370 371 407


-In this we check for all number from 0 to 1000 that it is armstrong or not by 'checkarmstrong' function, if number is armstrong then it return 1 otherwise return 0

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!