Search⌘ K
AI Features

Count the Number of Digits in an Integer

Explore methods to count digits in an integer by using division and logarithms, helping you understand decimal place values and improve problem-solving skills for coding interviews.

How to count the number of digits in an integer

Given a decimal number, continue dividing the number by ten until it reaches 0 and records the remainder at each step.

The resulting list of remainders is the equivalent place values of the Integer.

Example:

Input: 125

Output: 3

The illustration below explains the process of counting the digits in an integer.

Number Number = (number10\frac{number}{10}) Count
125 12 1
12 1 2
1 0 3

Problem statement

Given an integer, return the number of digits present in an integer input.

Example #1:

Input:  n = 125

Output: 3

Explanation: The number of digits present in input `n` is 3 (1, 2, 5).

Example #2:

Input:  n = 1256

Output: 4

Explanation: The number of digits present in input `n` is 4 (1, 2, 5, 6).

Solution

This is an example problem for counting digits in an integer. Solving this problem helps you find the place values and how they are represented in the decimal number system.

Let’s see some of the approaches we can take to solve this algorithmic problem.

Approach#1: Division [while loop]

In this program, while loop is iterated until the expression number != 0 is evaluated to 0 (false).

Let’s see the iterations for number = 125.

Iterations

  • After the first iteration, n(125) will be divided by 10, and its value will be 12, and the count is incremented to 1.

  • After the second iteration, the value of n(12) will be 1 and, the count is incremented to 2.

  • After the third iteration, the value of n(1) will be 0, and the count is incremented to 3.

Then the expression is evaluated to false, as n is 0, and the loop terminates.

class CountDigits {
private static int countDigits(int n){
int count = 0;
while (n > 0) {
++count;
n /= 10;
}
return count;
}
public static void main(String[] args) {
int number = 125;
System.out.println("Number of digits : " + countDigits(number));
}
}

Time and space complexities

Time complexity: O(n)

The run time depends on the number of digits in n. In the worst case, it iterates through all the digits until it becomes 0.

Space complexity: O(1)

The space complexity is O(1) since no additional space is allocated.

Approach#2: Logarithmic

We can use log10log_{10}(logarithm of base 10) to count the number of digits of positive numbers.

Digit count of N = upper bound of log10log_{10}(N).

Note: Logarithms are not defined for negative numbers.

log(0) is infinity

class CountDigits {
private static int countDigits(int n){
return n != 0 ? ((int) Math.floor(Math.log10(n) + 1)) : -1;
}
public static void main(String[] args) {
int number = 125;
System.out.println("Number of digits : " + countDigits(number));
}
}

Some of the other ways to achieve this are shown below.

Beware, these approaches are not recommended, as the time and space complexities are high.

Approach#3: Recursive

This recursive approach might be ineffective if we are dealing with a large integer n.

A recursive call adds a stack entry every time it runs and again when once it reaches the base condition. It then backtracks and executes each recursive function.

class CountDigits {
private static int helper(int n) {
// base checks
if (n == 0) {
return 0;
}
return (1 + helper(n / 10));
}
public static void main(String[] args) {
int number1 = 125;
int number2 = 00125;
//if input has any leading zeros, below line handles it.
int parsedNum2 = Integer.parseInt(Integer.toString(number2,8));
System.out.println("Number of digits : " + helper(number1));
System.out.println("Number of digits : " + helper(parsedNum2));
}
}

Approach#4: Convert to string

This is one of the ways to implement/solve the problem, but it is not recommended, as we are converting one type of data to another.

class CountDigits {
private static int helper(int n) {
// you can also use String.valueOf(number) to convert int to string.
String num = Integer.toString(n);
return num.length();
}
public static void main(String[] args) {
int number = 125;
System.out.println("Number of digits : " + helper(number));
}
}

Note: The string approach is just for your learning and understanding of Bitwise concepts. In the case that you pass number = 0, the above code returns 1 instead of 0, which is incorrect.