Kotlin Program to Count Number of Digits in an Integer

Kotlin Program to Count Number of Digits in an Integer: In this program, while loop is iterated until the test expression num != 0 is evaluated to 0 (false). After the first iteration, num will be divided by 10 and its value will be 345. Then, the count is incremented to 1.

Kotlin Program to Count Number of Digits in an Integer

Source Code

fun main(args: Array<String>) {
    var count = 0
    var num = 1234567

    while (num != 0) {
        num /= 10
        ++count
    }

    println("Number of digits: $count")
}

Output