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!!!!!!!!!!!!!!!!!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response