Count the Number of Digits in a Number
Count how many digits an integer contains using arithmetic only.
Divide the number by 10 repeatedly and count how many divisions it takes to reach 0. Each division removes one digit, so the count of divisions equals the number of digits. Handle 0 as a special case that has one digit.
Problem Statement
Given an integer, return how many digits it has. For example 12345 has 5
digits.
Count the digits of the absolute value so negatives match their positive
counterparts (-12345 also has 5 digits), and remember that 0 has exactly
one digit. Do not convert the number to a String.
Input: A single integer n.
Output: An integer — the count of digits in n.
Examples
Input: 12345
Output: 5There are five digits: 1, 2, 3, 4, 5.
Input: 0
Output: 1Zero is written with a single digit.
Constraints
-2,147,483,648 <= n <= 2,147,483,647No String conversion
Think Before You Code
Reveal the questions to ask yourself first
- What does dividing a number by 10 do to its digit count?
- How many times can you divide before you reach 0?
- What happens to your loop when the input is exactly 0?
- Do negative signs change how many digits a number has?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Count how many times you can divide the number by ten.
- Take the absolute value of
n. - If it is 0, return 1 immediately (zero has one digit).
- Set
count = 0. - While the value is not 0:
- Increment
count. - Divide the value by 10.
- Increment
- Return
count.
Dry Run
Walk through the example step by step
Counting the digits of n = 12345:
step | value | count
-----+-------+------
1 | 12345 | 1
2 | 1234 | 2
3 | 123 | 3
4 | 12 | 4
5 | 1 | 5
end | 0 | 5
Solution
Reveal the full Java solution
public class CountDigits {
public static int countDigits(int n) {
long value = Math.abs((long) n);
if (value == 0) {
return 1;
}
int count = 0;
while (value != 0) {
count++;
value /= 10;
}
return count;
}
public static void main(String[] args) {
System.out.println(countDigits(12345)); // 5
System.out.println(countDigits(0)); // 1
}
}
Integer division by ten strips exactly one digit each pass, so the number of
passes needed to reach 0 is the digit count. The explicit value == 0 guard is
essential: the loop condition value != 0 is false at the start for zero, so
without the guard the method would return 0 for an input that plainly has one
digit.
O(d) where d is the number of digits (log10 n)Space: O(1)Common Mistakes
- Returning 0 for the input 0 because the loop never executes.
- Using Math.abs on the int directly, overflowing for Integer.MIN_VALUE.
- Counting the minus sign as a digit for negative numbers.
Edge Cases to Test
- n = 0 must return 1.
- Negative numbers count the same digits as their positive form.
- Integer.MIN_VALUE (10 digits) needs a long for its absolute value.
Interview Follow-Ups
- How would you count digits without any loop, using Math.log10?
- How would you count only the non-zero digits?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →