English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Example of finding all Armstrong numbers between two integers. To solve this problem, we used nested loops and if statements.
To understand this example, you should understand the followingPython programmingTopic:
A positive integer is called an Armstrong order, n if
abcd... = an + bn + cn + dn + ...
For example,
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.
Visit this page to learn howCheck if a number is an Armstrong number in Python.
# Python program to find Armstrong numbers in integers lower = 100 upper = 2000 for num in range(lower, upper + 1) # order number order = len(str(num)) # Initialize sum sum = 0 temp = num while temp > 0: digit = temp % 10 sum += digit ** order temp //= 10 if num == sum: print(num)
Output result
153 370 371 407 1634
Here, we set the lower limit in the variable lower100, and the upper limit was set in the variable upper2000. We used a for loop to iterate from the variable lower to upper. In the iteration, the value of lower increases1, and check if it is an Armstrong number.
You can change the range and test by changing the variables lower and upper. Please note that the variable lower should be less than upper for this program to run properly.