How to count total number of digits from a given positive number without looping in C?
views:
303answers:
3
+5
A:
For integers, take the log10 of the number, round down, and add one.
TEST:
#include <math.h>
#include <stdio.h>
int
num_digits(unsigned long number)
{
return (int)(floor(log10((double)number)) + 1.0);
}
int
main(int argc, char **argv)
{
unsigned long test_numbers[] = {
1, 9, 10, 99, 100, 999, 1000, 9999, 10000, 99999, 100000, 999999,
123456789ul,
999999999ul,
0
};
unsigned long *ptr;
for(ptr = test_numbers; *ptr; ptr++)
{
printf("Number of digits in %lu: %d\n", *ptr, num_digits(*ptr));
}
return 0;
}
Output:
Number of digits in 1: 1
Number of digits in 9: 1
Number of digits in 10: 2
Number of digits in 99: 2
Number of digits in 100: 3
Number of digits in 999: 3
Number of digits in 1000: 4
Number of digits in 9999: 4
Number of digits in 10000: 5
Number of digits in 99999: 5
Number of digits in 100000: 6
Number of digits in 999999: 6
Number of digits in 123456789: 9
Number of digits in 999999999: 9
ptomato
2010-06-01 10:05:03
Also, tag your homework assignments with the 'homework' tag.
ptomato
2010-06-01 10:05:20
Are you sure this is precise enough, and doesn't give the wrong answer for some 999... number?
starblue
2010-06-01 11:11:35
@starblue: you can't be entirely sure based on the C standard, since it doesn't specify the accuracy of log functions or the widths of types. However, if the "given positive number" is a 32 bit integer, and the log is done in an IEEE double, then the *precision* is good enough. The *accuracy* may not be.
Steve Jessop
2010-06-01 11:47:24
See edit for a small test program.
ptomato
2010-06-01 11:57:22
A:
Two solutions for you!
int digit_count(unsigned int n)
{
if (n < 10)
return 1;
else
return (1 + digit_count(n / 10));
}
and
unsigned int n = 50;
int i = 0;
HACK:
i++;
if (n < 10)
{
printf("Digits: %d\n", i);
}
else
{
n /= 10;
goto HACK;
}
Don't hate me for the last one :(
LukeN
2010-06-01 10:08:12
"Without looping" should include the use of "goto". Now you are hated by everyone ;-)
tur1ng
2010-06-01 10:14:46
Well, "goto" can be used for multiple things, including but not limited to loops ;)
LukeN
2010-06-01 10:17:32
Technically speaking, so does the first one - recursion is mathematically equivalent to looping. It all depends on what definition of "looping" you want to use. Is it just while/do-while/for? Or is it any construct that repeats an action if a sentinel case has not yet been met?
Wayne Werner
2010-06-01 13:02:25
@Shravan: that's exactly the reason WHY I called it "HACK" :) - @Wayne Werner - because OP wasn't clear enough, I just thought he was meant to avoid the typical "while", "for", ... :)
LukeN
2010-06-01 13:16:22
+1
A:
One possible solutions, assuming 16-bit integer values and logical expressions evaluating to 0 or 1. You could replace the conditions with (u > 99) ? 1 : 0
if you're worried about portability.
int digits( unsigned u)
{
return 1 + (u > 9) + (u > 99) + (u > 999) + (u > 9999);
}
tomlogic
2010-06-01 15:09:39