English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Comprehensive Collection of Kotlin Examples
In this program, you will learn how to display all Armstrong numbers between two given intervals (low and high) in Kotlin.
A positive integer is called an n-th order Armstrong number if
abcd... = an + bn + cn + dn + ...
For3digit Armstrong number, the sum of the cubes of each digit equals the number itself. For example:
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number
This program is based onHow to check if an integer is an Armstrong numberconcept.
fun main(args: Array<String>) { val low = 999 val high = 99999 for (number in low + 1..high - 1) { var digits = 0 var result = 0 var originalNumber = number //Number of digits calculation while (originalNumber != 0) { originalNumber /= 10 ++digits } originalNumber = number //The result includes the nth power of its digits while (originalNumber != 0) { val remainder = originalNumber % 10 result += Math.pow(remainder.toDouble(), digits.toDouble()).toInt() originalNumber /= 10 } if (result == number) print("$number ") } }
When running the program, the output is:
1634 8208 9474 54748 92727 93084
In the above program, each number between the given high and low intervals was checked.
After each check, digits and result will be restored to 0.