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);
}

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);
}

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);
}

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);
}

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

Did you find this article valuable?

Support Deepak Chhitarka by becoming a sponsor. Any amount is appreciated!