Count Digits
Java coding interview problem for Basic Number Programs: Count Digits.
Counting the number of digits in an integer is one of the most frequently asked Java coding interview questions. It helps interviewers evaluate your understanding of loops, arithmetic operations, integer manipulation, and problem-solving skills.
This problem serves as the foundation for many other number-based coding questions such as Armstrong Number, Palindrome Number, Reverse Integer, and Sum of Digits.
What is Count Digits?
Count Digits means determining how many digits are present in a given number.
For example,
Input
12345
Digits
1
2
3
4
5
Output
5
Example 1
Input
987654
Output
6
Example 2
Input
1000
Output
4
Example 3
Input
7
Output
1
Example 4
Input
0
Output
1
Note: Zero has exactly one digit.
Real Interview Question
Write a Java program to count the number of digits in an integer without converting it into a String.
Understanding the Logic
The idea is straightforward.
Every time we divide a number by 10, its last digit is removed.
For example,
12345
becomes
1234
↓
123
↓
12
↓
1
↓
0
The number becomes zero after 5 divisions.
Therefore,
Number of Digits = 5
Brute Force Approach
The simplest solution is
- Initialize a counter.
- Divide the number by 10 repeatedly.
- Increase the counter after every division.
- Continue until the number becomes zero.
Algorithm
Step 1
Read the input number.
Step 2
Handle the special case.
If number == 0
Return 1
Step 3
Initialize
count = 0;
Step 4
Repeat while
number != 0
Step 5
Increase the counter.
count++;
Step 6
Remove the last digit.
number = number / 10;
Step 7
Print the total count.
Dry Run
Input
12345
| Iteration | Number | Count |
|---|---|---|
| 1 | 12345 | 1 |
| 2 | 1234 | 2 |
| 3 | 123 | 3 |
| 4 | 12 | 4 |
| 5 | 1 | 5 |
| End | 0 | 5 |
Output
5
Another Dry Run
Input
567890
| Iteration | Number | Count |
|---|---|---|
| 1 | 567890 | 1 |
| 2 | 56789 | 2 |
| 3 | 5678 | 3 |
| 4 | 567 | 4 |
| 5 | 56 | 5 |
| 6 | 5 | 6 |
| End | 0 | 6 |
Output
6
Approach 1 — Using Arithmetic Operators
This is the most common and interview-preferred solution.
Complete Java Program
public class CountDigits {
public static void main(String[] args) {
int number = 12345;
if (number == 0) {
System.out.println("Number of Digits = 1");
return;
}
int count = 0;
while (number != 0) {
count++;
number = number / 10;
}
System.out.println("Number of Digits = " + count);
}
}
Output
Number of Digits = 5
Step-by-Step Code Explanation
Step 1
Declare the input number.
int number = 12345;
Current value
12345
Step 2
Handle the special case.
if (number == 0) {
System.out.println("Number of Digits = 1");
return;
}
Since
0
contains one digit,
its answer should be
1
Step 3
Initialize the counter.
int count = 0;
Initially
Count = 0
Step 4
Run the loop.
while (number != 0)
The loop continues until all digits are removed.
Step 5
Increase the count.
count++;
Every iteration processes exactly one digit.
Step 6
Remove the last digit.
number = number / 10;
Example
12345
↓
1234
↓
123
↓
12
↓
1
↓
0
Each division removes one digit from the right.
Step 7
Print the answer.
System.out.println("Number of Digits = " + count);
Output
Number of Digits = 5
Why Does This Work?
The algorithm relies on the fact that integer division by 10 removes the last digit.
For example,
98765
becomes
9876
↓
987
↓
98
↓
9
↓
0
Each division removes one digit.
By counting the number of divisions until the number becomes zero, we obtain the total number of digits.
Advantages of This Approach
- Easy to understand.
- Uses only arithmetic operators.
- No extra data structures are required.
- Works efficiently for integers.
- Preferred in Java coding interviews.
- Uses constant extra memory.
Drawbacks
Although this solution is efficient and widely accepted, interviewers often ask follow-up questions such as:
- Can you solve it using a reusable method?
- Can you solve it using logarithms?
- Can you solve it using String conversion?
- How would you handle negative numbers?
- Which approach is the fastest?
we'll cover:
- Reusable method approach
- Logarithmic (
Math.log10()) approach - String-based approach
- Time and space complexity
- Comparison of all approaches
- Common interview mistakes
- Frequently asked interview questions
- Related coding problems
- Key takeaways
- Interview tips
Approach 2 — Using a Reusable Method
Instead of writing the logic inside the main() method, we can create a reusable method.
This approach improves
- Code reusability
- Readability
- Unit testing
- Maintainability
Java Program
public class CountDigitsMethod {
static int countDigits(int number) {
if (number == 0) {
return 1;
}
number = Math.abs(number);
int count = 0;
while (number != 0) {
count++;
number = number / 10;
}
return count;
}
public static void main(String[] args) {
int number = 987654;
System.out.println("Number of Digits = " + countDigits(number));
}
}
Output
Number of Digits = 6
Approach 3 — Using Math.log10()
This approach uses a mathematical formula.
Formula
Digits = floor(log10(number)) + 1
Example
Number = 12345
log10(12345) = 4.09
floor(4.09) = 4
4 + 1 = 5
Java Program
public class CountDigitsLogarithm {
public static void main(String[] args) {
int number = 12345;
if (number == 0) {
System.out.println("Number of Digits = 1");
} else {
int digits = (int) Math.floor(Math.log10(Math.abs(number))) + 1;
System.out.println("Number of Digits = " + digits);
}
}
}
Output
Number of Digits = 5
Approach 4 — Using String Conversion
Although interviewers usually expect an arithmetic solution,
Java also allows counting digits by converting the number into a String.
Java Program
public class CountDigitsString {
public static void main(String[] args) {
int number = -12345;
int count = String.valueOf(Math.abs(number)).length();
System.out.println("Number of Digits = " + count);
}
}
Output
Number of Digits = 5
Time Complexity
Arithmetic Solution
| Operation | Complexity |
|---|---|
| Time | O(log₁₀ n) |
| Space | O(1) |
Logarithmic Solution
| Operation | Complexity |
|---|---|
| Time | O(1) |
| Space | O(1) |
String Solution
| Operation | Complexity |
|---|---|
| Time | O(log₁₀ n) |
| Space | O(log₁₀ n) |
Comparison of All Approaches
| Approach | Time | Space | Recommended |
|---|---|---|---|
| Arithmetic Operators | O(log n) | O(1) | ✅ Best for Interviews |
| Reusable Method | O(log n) | O(1) | Reusable |
| Math.log10() | O(1) | O(1) | Fastest |
| String Conversion | O(log n) | O(log n) | Easy but Less Preferred |
Common Mistakes
Mistake 1
Ignoring the special case
0
Wrong
int count = 0;
while(number != 0){
count++;
}
Output
0
Correct Output
1
Mistake 2
Not handling negative numbers.
Example
-12345
Always use
number = Math.abs(number);
before processing.
Mistake 3
Using
Math.log10()
for zero.
Wrong
Math.log10(0)
This is undefined.
Always check
number == 0
first.
Mistake 4
Forgetting to divide the number.
Wrong
count++;
Correct
count++;
number = number / 10;
Otherwise,
the loop never terminates.
Mistake 5
Using String conversion when the interviewer specifically asks
"Without converting the number into a String."
Use arithmetic operators instead.
Interview Follow-up Questions
Q1. Count digits without using String.
Q2. Count digits recursively.
Q3. Count even digits.
Q4. Count odd digits.
Q5. Count occurrences of a particular digit.
Q6. Count zeros in a number.
Q7. Count digits in a very large number.
Q8. Explain the logarithmic approach.
Q9. Compare all approaches.
Q10. Which solution would you use in production?
Related Coding Problems
- Sum of Digits
- Reverse Integer
- Armstrong Number
- Palindrome Number
- Product of Digits
- Largest Digit
- Smallest Digit
- Digital Root
Key Takeaways
- Count digits by repeatedly dividing the number by 10.
- Integer division removes one digit during each iteration.
- Handle 0 as a special case because it contains exactly one digit.
- Use
Math.abs()to correctly process negative numbers. Math.log10()provides a constant-time mathematical solution but requires special handling for zero.- The arithmetic solution is the most common and interview-friendly approach.
Interview Tip
If an interviewer asks:
"Write a Java program to count the number of digits in an integer."
Start with the arithmetic solution because it demonstrates your understanding of integer manipulation using % and /. Mention the Math.log10() solution as an optimization and discuss its limitation with zero. Finally, explain the String-based approach as an alternative, noting that it is simpler but usually not preferred when the interviewer expects an arithmetic solution. Demonstrating multiple approaches and their trade-offs shows strong problem-solving ability and practical Java knowledge.