Digital Root in Java: Explanation & Practice
Find the digital root of a number
Problem summary
Keep summing digits until you get a single digit (e.g., 9875 → 29 → 11 → 2).
Starter code
public class Main {
public static void main(String[] args) {
int number = 9875; // Test case 1
// Calculate digital root
// Print: Digital root: <result>
}
}Expected output and test cases
- 9875 → 29 → 11 → 2
Digital root: 2
- 99999 → 45 → 9
Digital root: 9
- 5
Digital root: 5
Hints
- Outer loop: while number >= 10
- Inner loop: sum all digits
- Replace number with digit sum
Related Control Flow exercises
- Practice Diamond Pattern in Java
- Practice Collatz Conjecture in Java
- Practice Abundant Number Check in Java
Practice all Control Flow exercises · Run this idea in the Java compiler