# Thread: decimal to number if digits in different bases

1. ## decimal to number if digits in different bases

anyone knows how they would start this off?

I need a which will take a decimal positive integer x then the program prints the number of digits required to represent the decimal integer x in bases 2, 4, 8, and 16.

For example: If the input is 18, then the output should be:

Given decimal number: 18
Number of digits required in base 2: 5
Number of digits required in base 4: 3
Number of digits required in base 8: 2
Number of digits required in base 16: 2

any help will be appreciated

2. You could use tables, or a division loop (this is the "bad at math" approach).

You could use exponents/logarithms (this is the "good at math" approach).

3. how exactly would it start though?

4. I would start with a piece of paper and work out a psuedocode algorithm. Since the problem is using the traditional CS bases (2, 4, 8, 16) I would probably draw upon my knowledge of binary.

5. alright thanks. i got how to do it

6. Originally Posted by tabstop
You could use tables, or a division loop (this is the "bad at math" approach).

You could use exponents/logarithms (this is the "good at math" approach).
However, unless the numbers are really huge, it's probably faster to do the "bad at math" method than to calculate the log(X)/log(Y) - even if you can-precalculate the log(Y).

--
Mats

7. ## i cant figure out whats wrong with it any help please

Code:
```#include <stdio.h>
#include <math.h>

int main()
{
int x, a, b, c, d;

scanf("%d", &x);

a=1+(log(x)/log(2));
b=1+(log(x)/log(4));
c=1+(log(x)/log(8));
d=1+(log(x)/log(16));

printf("Given decimal number: %d\n", x);
printf("Number of digits required in base 2: %d\n", a);
printf("Number of digits required in base 4: %d\n", b);
printf("Number of digits required in base 8: %d\n", c);
printf(" Number of digits required in base 16: %d", d);

}```

8. What do you think is wrong? Or, put another way, how is the result differnet from what you expect?

--
Mats

9. it just doesnt compile. it gives me this when i try to compile it. i am using unix

> gcc num-digits.c
Undefined first referenced
symbol in file
log /var/tmp//ccNuKjnz.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

10. I'm pretty sure it doesn't matter, but can you try renaming the file to something without '-' in the name? If there's an undefined name in the input, it should tell you.

I copied and pasted your code onto my machine and compiled it with gcc-mingw (I have a windows machine), and it works just fine.

--
Mats

11. yeah it works on windows but it doesnt want to compile on unix. hmm i wonder what it is

12. can you do "od -t x1 numdigits.c | head > numbytes.dump" and post the contents of numbytes.dump here?

--
Mats

13. is this what you wanted. i'm a new at this so dont much.
Code:
```jgnlez@shellfish:~/ece175> od -t x1 num-digits.c | head > numbytes.dump
jgnlez@shellfish:~/ece175> cat numbytes.dump
0000000 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e
0000020 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 6d 61 74
0000040 68 2e 68 3e 0a 0a 69 6e 74 20 6d 61 69 6e 28 29
0000060 0a 7b 20 0a 09 69 6e 74 20 78 2c 20 61 2c 20 62
0000100 2c 20 63 2c 20 64 3b 0a 09 0a 09 70 72 69 6e 74
0000120 66 28 22 50 6c 65 61 73 65 20 65 6e 74 65 72 20
0000140 61 20 6e 75 6d 62 65 72 3e 22 29 3b 0a 09 73 63
0000160 61 6e 66 28 22 25 64 22 2c 20 26 78 29 3b 0a 0a
0000200 09 61 3d 31 2b 28 6c 6f 67 28 78 29 2f 6c 6f 67
0000220 28 32 29 29 3b 0a 09 62 3d 31 2b 28 6c 6f 67 28```

14. I think I know what the problem is - you need to link to the math library.

Try adding -lm at the end of the gcc command.

--
Mats

15. that made it work. but how do i make it work without adding -lm nevermind i got it thanks.