Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Implement a program to calculate the sum of all digits in a number until a single digit is obtained (digital root).

Medium

Problem 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)}") }
Log in or sign up to run or submit your code.Log inSign up
Input
A hardcoded number: 493193
Expected Output
The digital root is: 2