How to count number of digits ?

The good approach will be to use log10 Time complexity O(1) Space complexity O(1) . The number of digits in an integer = the upper bound of log10(n).
Example: log10(12345.0)) = > 4.091491094267951 so we have to use 4+1

print ("count the number of digits = ", Int(log10(12345.0))+1)// prints 5

Read more about Logarithm here https://janeshswift.com/ios/dsa/math/logarithms-explained/

Leave a Comment

Your email address will not be published. Required fields are marked *