Armstrong number
Armstrong Number
A number is called as Armstrong number if sum
of cubes of digits of number is equal to the number itself.
For example: 153,371 etc.
153 is a Armstrong number since
1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153
n=int(input("Enter the number:"))
s=0
k=n
while(n>0):
d=n%10
s=s+d**3
n=int(n/10)
if(s==k):
print("This is an Armstrong number")
else:
print("This is not an Armstrong number")
Example 1:
#program to check a number is an Armstrong number or notn=int(input("Enter the number:"))
s=0
k=n
while(n>0):
d=n%10
s=s+d**3
n=int(n/10)
if(s==k):
print("This is an Armstrong number")
else:
print("This is not an Armstrong number")
Output:
Enter the number:153
This is an Armstrong number
>>>
for i in range(100,1001):
n=i
k=n
s=0
while(n>0):
d=n%10
s=s+d**3
n=int(n/10)
if(s==k):
print(i)
370
371
407
>>>
This is an Armstrong number
>>>
Example 2:
#program to display the the Armstrong number between 100 to 1000for i in range(100,1001):
n=i
k=n
s=0
while(n>0):
d=n%10
s=s+d**3
n=int(n/10)
if(s==k):
print(i)
Output:
153370
371
407
>>>
Click the below link to watch the video on Armstrong number
Program to display Armstrong number
Dry Run:
Assume n=153
s=0 [initial
value]
n
|
d
= n%10
|
s
= s+d**3
|
153%10=3
|
0+27=27
|
|
153/10=15
|
15%10=5
|
27+125=152
|
15/10=1
|
1%10=1
|
152+1=153
|
1/10=0
|
Comments
Post a Comment
If you have any doubt, Please let me know..