DEV Community

Deepak Chhitarka
Deepak Chhitarka

Posted on • Updated on

How to count number of digits in a number?

Counting number of digits in a number is one of the most common and first problem one encounters while doing competitive programming. No language has built in functionality to perform this task, so programmers have to manually find it out and yet many new programmers does not know how to find the number of digits in an integer.

It is considered a fundamental problem and is one of the first problem taught if you are doing a DSA course as this can be handy in multiple of questions that you must be aware of how to find the number of digits quickly. It covers maths fundamentals as well as programming basics and is a nice first step if you are new to competitive programming as it can be challenging for beginners.

I will be using C++ code to demonstrate how to count number of digits in a number as C++ is the preferred language when it comes to competitive programming.

I will be covering 4 different approaches to solve this problem

1. Iterative Solution

In this, we have a count variable and we loop over the number while incrementing the count variable. In each iteration, we divide the number by 10 and update the number with the quotient.

#include <bits/stdc++.h>

int numOfDigits(long long n)
{
    int count = 0; 
    while (n != 0) { 
        n = n / 10; 
        ++count; 
    } 
    return count;
}

int main()
{
    long long n;
    cout<<"Enter the number: ";
    cin>>n;
    cout<<numOfDigits(n);
}
Enter fullscreen mode Exit fullscreen mode

2. Recursive Solution -

This solution is somewhat similar to the iterative solution we just discussed. In this, the base condition is when the number becomes less than 10. We just increment the count and return it. In our function, we pass 2 parameters, the number and the count variable. Each function returns it's count parameter + 1. In this way we can find the no of digits in the number.

#include <bits/stdc++.h>

int numOfDigits(long long n)
{
    if (n < 10) 
      return 1; 
    return 1 + numOfDigits(n / 10);
}

int main()
{
    long long n;
    cout<<"Enter the number: ";
    cin>>n;
    cout<<numOfDigits(n);
}
Enter fullscreen mode Exit fullscreen mode

3. Log Based Solution -

This solution is pure mathematical solution. We can calculate the number of digits by just taking the log of that number with base 10 + 1 on the condition that the number is greater than 0. We can use a ternary operator to check.

#include <bits/stdc++.h>

int numOfDigits(long long n)
{
    return floor(log10(n) + 1);
}

int main()
{
    long long n;
    cout<<"Enter the number: ";
    cin>>n;
    cout<<numOfDigits(n);
}
Enter fullscreen mode Exit fullscreen mode

4. Convert to string and find it's length -

This solution is the simplest one in my opinion as we just convert the number into string and then find the length of the string and return it. Almost all programming languages have built-in methods to find string's length and also convert integer to string.

#include <bits/stdc++.h>

int numOfDigits(long long n)
{
    string num = to_string(n); 
    return num.size();
}

int main()
{
    long long n;
    cout<<"Enter the number: ";
    cin>>n;
    cout<<numOfDigits(n);
}
Enter fullscreen mode Exit fullscreen mode

It is my first time writing blogs, so I would love to hear your feedback. You can find the original and other articles on https://codeunlock.in/

Top comments (4)

Collapse
 
christianparpart profile image
Christian Parpart

What about ceil(log10(n))
or -int(-log10(n))

I find both of them know beautiful. :)

Collapse
 
dchhitarka profile image
Deepak Chhitarka

This is for positive integers only, or we can just take the abs value of the number as the number of digits will remain same whether it is +x or -x

Collapse
 
dchhitarka profile image
Deepak Chhitarka

Yes, I think I forgot to consider 0. We will need to check for 0 in solution 1 and 3, while 2 and 4 will work fine

Collapse
 
kiseque profile image
André Fertig de Oliveira

Typo in number 4, should be "its" not "it's".
Nice article!