Competitive Programming Questions |
In this article, we are going to find out the Armstrong number in the range of 1 to 1000 in Python. For this, we are going to use a For loop and a while loop inside it.
An Armstrong number is a number whose sum of the cubes of the digits is equal to the number itself. For example, 153 = 1^3 + 5^3 + 3^3
If you are looking for a Python Tutorial Series, then it's here.
def armstrongNum():
for i in range(1, 1001):
sum = 0
temp = i
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if i == sum:
print('The armstrong Number in the range 1 to 1000 is: ',i)
armstrongNum()
The output of the python program is:
The armstrong Number in the range 1 to 1000 is: 1
The armstrong Number in the range 1 to 1000 is: 153
The armstrong Number in the range 1 to 1000 is: 370
The armstrong Number in the range 1 to 1000 is: 371
The armstrong Number in the range 1 to 1000 is: 407
Comments
Post a Comment