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

  1. Outer loop: while number >= 10
  2. Inner loop: sum all digits
  3. Replace number with digit sum

Related Control Flow exercises

Practice all Control Flow exercises · Run this idea in the Java compiler