Implement a program to calculate the sum of all digits in a number until a single digit is obtained (digital root).
MediumProblem Statement
Implement a program to calculate the sum of all digits in a number until a single digit is obtained (digital root).
Sample Input
A hardcoded number: 493193
Sample Output
The digital root is: 2
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
fun digitalRoot(num: Int): Int {
return if (num < 10) num
else digitalRoot(num.toString().map { it - '0' }.sum())
}
fun main() {
val number = 493193
println("The digital root is: ${digitalRoot(number)}")
}
Input
A hardcoded number: 493193
Expected Output
The digital root is: 2