Finding No of Digits in a Number

Prasad Bylapudi
1 min readMar 13, 2020

Now we are going to look at the different ways to find no of digits in a number

  1. Iterative solution
  2. Recursive solution
  3. logarithmic solution

Iterative solution

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

2.Recursive Solution

int count_digits(long n)
{
if(n==0):
return 0;
else
return 1+ count_digits(n/10);}

3. Logarithmic solution

int count_digits(long n)
{
return floor(log_₁₀n +1);
}#here we use that log_₁₀n it's value is 2.08 so,we use floor to terminate the decimal points and add one to the value so we get the result

The iterative and recursive solutions are common and but the logarithmic solution is the fastest of all these.

I hope it helps you!

Happy Reading!!!!!!!!!!!!!!!!!

--

--

Prasad Bylapudi

👨‍💻 Tech Enthusiast 🌟 | Code Connoisseur 🚀 | Digital Explorer 🌐| Software Engineer