In this tutorial, you shall learn how to write a program to find the product of digits in a given number in Kotlin.

Kotlin – Product of Digits Program

To find the product of digits in a given number in Kotlin, iterate over the digits of the number, and accumulate the product.

To iterate over the digits of a number, you can use while loop statement, where we divide the number by 10, and get the remainder, which is a digit from the number, and then update the number with the quotient of given number divided by 10.

Program

In the following program, we read a number from user and store it in num, then find the product of the digits in the number, and finally print the product to output.

Main.kt

</>
Copy
fun main() {
    print("Enter an integer : ")
    val num = readLine()!!.toInt()

    var sum = 0
    var temp = num
    while (temp != 0) {
        val digit = temp % 10
        sum += digit
        temp /= 10
    }

    println("The sum of digits in $num is $sum.")
}

Output #1

Enter an integer : 412
The product of digits in 412 is 8.

Output #2

Enter an integer : 123456789
The product of digits in 123456789 is 362880.

Output #3

Enter an integer : 10236
The product of digits in 10236 is 0.

Related Tutorials

Conclusion

In this Kotlin Tutorial, we learned how to find the product of digits in a given number.