Kotlin Program to Display Armstrong Number Between Two Intervals

A positive integer is called an Armstrong number of order n if

abcd... = an + bn + cn + dn + ...

In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example:

153 = 1*1*1 + 5*5*5 + 3*3*3  // 153 is an Armstrong number.

This program is built on the concept of how to check whether an integer is an Armstrong number or not.

Example: Armstrong Numbers Between Two Integers

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

        // result contains sum of 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 you run the program, the output will be:

1634 8208 9474 54748 92727 93084 

In the above program, each number between the given interval high and low are checked.

After each check, number of digits and sum result are restored to 0.

Did you find this article helpful?