The problem is that it’s returning one int
which has a limited capacity. I changed the code to use long
which will allow larger numbers. But it has limit too.
So what’s the problem? Trying to make a numerical representation as if it were a number. The number is the number, you do math with it. The numerical representation is what you see written. It’s a text that happens to have numeric digits written down, but that’s not a number.
You don’t do accounts with representations. And showing the number in binary is just a representation. Just like what you see on the screen is usually just a decimal representation.
If you really want to create a binary representation, generate a text and not a number, change the return to char *
and encode in an appropriate way to generate the text. As was done in another question.
#include <stdio.h>
long binario(int num) {
if (num == 0) return 0;
return (num % 2) + 10 * binario(num / 2);
}
int main() {
printf("%ld", binario(1025));
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.