0
0
JavaProgramBeginner · 2 min read

Java Program to Count Digits in Number

To count digits in a number in Java, use a loop that divides the number by 10 repeatedly and increments a counter until the number becomes 0, like while (num != 0) { count++; num /= 10; }.
📋

Examples

Input12345
Output5
Input0
Output1
Input1000000
Output7
🧠

How to Think About It

To count digits, think of how many times you can divide the number by 10 before it becomes zero. Each division removes the last digit, so counting these divisions tells you how many digits the number has.
📐

Algorithm

1
Get the input number.
2
If the number is 0, return 1 because it has one digit.
3
Initialize a counter to 0.
4
While the number is not 0, divide it by 10 and increment the counter.
5
Return the counter as the number of digits.
💻

Code

java
public class DigitCount {
    public static void main(String[] args) {
        int num = 12345;
        int count = 0;
        int temp = num;
        if (temp == 0) {
            count = 1;
        } else {
            while (temp != 0) {
                count++;
                temp /= 10;
            }
        }
        System.out.println("Number of digits in " + num + " is: " + count);
    }
}
Output
Number of digits in 12345 is: 5
🔍

Dry Run

Let's trace the number 12345 through the code to count its digits.

1

Initialize variables

num = 12345, count = 0, temp = 12345

2

Check if temp is 0

temp is 12345, so not 0, skip count=1

3

Start loop: temp != 0

count=0, temp=12345

4

First iteration

count=1, temp=12345/10=1234

5

Second iteration

count=2, temp=1234/10=123

6

Third iteration

count=3, temp=123/10=12

7

Fourth iteration

count=4, temp=12/10=1

8

Fifth iteration

count=5, temp=1/10=0

9

Loop ends

temp=0, exit loop

10

Print result

Number of digits in 12345 is: 5

Iterationcounttemp
111234
22123
3312
441
550
💡

Why This Works

Step 1: Handle zero input

If the number is 0, it has exactly one digit, so we set count to 1 immediately.

Step 2: Divide to remove digits

Dividing the number by 10 removes its last digit, so each division counts one digit.

Step 3: Count divisions

We keep dividing and counting until the number becomes 0, which means all digits are counted.

🔄

Alternative Approaches

Convert number to string and count length
java
public class DigitCount {
    public static void main(String[] args) {
        int num = 12345;
        String str = Integer.toString(num);
        System.out.println("Number of digits in " + num + " is: " + str.length());
    }
}
This method is simpler but uses extra memory for the string and may be slower for very large numbers.
Use logarithm to count digits
java
public class DigitCount {
    public static void main(String[] args) {
        int num = 12345;
        int count = (num == 0) ? 1 : (int) Math.log10(num) + 1;
        System.out.println("Number of digits in " + num + " is: " + count);
    }
}
This method is very fast but only works for positive numbers and requires math functions.

Complexity: O(d) time, O(1) space

Time Complexity

The loop runs once for each digit, so time grows linearly with the number of digits, O(d).

Space Complexity

Only a few variables are used, so space is constant, O(1).

Which Approach is Fastest?

The logarithm method is fastest for large numbers but less intuitive; the loop method is simple and reliable.

ApproachTimeSpaceBest For
Loop divisionO(d)O(1)All integers, simple logic
String conversionO(d)O(d)Quick coding, small numbers
Logarithm methodO(1)O(1)Fastest for large positive numbers
💡
Remember to handle the special case when the number is zero, as it has one digit.
⚠️
Beginners often forget to handle zero and get a digit count of zero instead of one.