← Coding round practice

Armstrong Number

MediumNumbers & Math

The problem

An Armstrong number equals the sum of each of its digits raised to the power of the number of digits (e.g. 153 = 1^3 + 5^3 + 3^3).

Sample input

153

Sample output

true
Show the solution · 8 languages
fun isArmstrong(n: Int): Boolean {
    val digits = n.toString()
    val power = digits.length
    var total = 0
    for (ch in digits) {
        total += Math.pow((ch - '0').toDouble(), power.toDouble()).toInt()
    }
    return total == n
}

fun main() {
    println(isArmstrong(153))
}