My teacher said there are two ways to find the # of digits of a number but i dont get either of them
what does both of the codes mean?
1 2 3 4

log 10 ( abs(n)) + 1
if n=0 d =1
else
d= log 10 ( abs(n))
 
the other one:
1 2 3 4 5 6

absn= abs(n);
d= 1;
while ( abs(n) / 10 > 10 )
d++;
absn = absn /10;
}
 
Last edited on
In the first one, he's using the fact that the base 10 logarithm of a number besides 0 is one less than its number of digits.
In the second one, he's seeing how many times he can divide the number by 10; this is its number of digits.
Try it out and see for yourself.
Edit: here's a simple Python snippet that demonstrates this:
http://ideone.com/iC8sPR
Last edited on