X1614: Count the digits of an integer

Description

Write a recursive method int countDigits(int n) that returns how many digits are in n. The key idea over recursion in this method is:

  • Base case: n < 10 → return 1
  • Recursive case: 1 + countDigits(n / 10)

Examples:

countDigits(174) -> 3
countDigits(2025) -> 4

Your Answer:

Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.