In this article we will discuss four methods to count the digits in a given number. The codes provided will be in C++, but same logic can be used to implement in any other language.

Table of Contents

**Iterative Solution** to Count Digits in a Number

This method uses division operation(/) to pluck the digits from a number, simultaneously incrementing the count variable till the number become zero. Below is the simple C++ implementation-

```
// Iterative Solution to Count Digits in a Number in C++
#include <bits/stdc++.h>
using namespace std;
int countDigits(int n)
{
int count = 0;
while (n != 0)
{
n = n / 10;
count++;
}
return count;
}
// Main function
int main(void)
{
int n = 8564;
cout << "Number of digits in"<< n <<": "<<countDigits(n);
return 0;
}
```

```
Dry Run
1. Let's take n = 8564
2. In the 1st iteration, n become 856 and count become 1
3. In the 2nd iteration, n become 85 and count become 2
4. In the 3rd iteration, n become 8 and count become 3
5. In the 4th iteration, n become 0 and count become 4
6. Loop will break as n become zero.
7. The value of count will be returned(4 in this case) to main function.
```

**Time Complexity:** Theta(number of digits in the number) = Theta(log n)

Space Complexity = Auxiliary Space = Theta(1)

**Recursive Solution** to Count Digits in a Number

The logic or idea of recursive solution to count digits in a number is same as iterative method above. Here we will just use recursion in place of loop.

```
// Recursive solution to Count Digits in a Number in C++
#include <bits/stdc++.h>
using namespace std;
int countDigits(int n)
{
if (n == 0)
return 0;
return 1 + countDigits(n / 10);
}
```

We keep removing the last digit of the number and simultaneously adding 1 to the return value until we reach the base condition.

**Logarithmic Solution** to Count Digits in a Number

This method uses the idea that floor of log10(log with base 10) of a number + 1, returns the count of digits of that number. That’s what we want, so we just need to return the floor of log10 of the provided number.

The limitation of direct use of this method is that, it will only be correct in case of positive numbers as log of negative numbers are not defined. But, we can do some modification in the code to make it correct in all case.

```
// Logarithmic solution to Count Digits in a Number in C++
int countDigits(int n) {
// Converting -ve numbers to +ve
if(n < 0)
n *= -1;
return floor(log10(n) + 1);
}
```

**String Based Solution** to Count Digits in a Number

This method can be used languages which supports string data type.

The idea behind this method is: we will first convert the number into an string. Then we will just count and return the number of characters in that string.

Remember we also need to handle negative number input as above to make this method works for all integers.

```
// String Based Solution to Count Digits in a Number
int countDigits(int n)
{
if(n < 0)
n *= -1;
// converting number to string using to_string() in C++
string num = to_string(n);
// returning the count of characters string(equal to number of digits in n)
return num.size();
}
```