1
My code works up to a point. When I provide values above 1023 for num, the program goes wrong, below 1023 the program works perfectly. Someone knows what can be?
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int bin = 0, num = 0, resp = 0, cont = 0;
    printf("digite um numero\n");
    scanf("%d", &num);
    do
    {
        bin = (bin * 10) + (num % 2);
        num = num / 2;
        cont++;
    }
    while (num > 0);
    do
    {
        cont--;
        resp = (resp * 10) + (bin % 2);
        bin = (bin - (bin % 2)) / 10;
    }
    while (cont > 0);
    printf("%d", resp);
    return 0;
}
						
You can select the code and press
<ctrl> + kto format the code properly– Jefferson Quesado
Thanks for the info! I made the changes already.
– Lucas
You can take a look at this solution: Decimal to Binary in C that stores the value in a string.. or simply print to the screen (depending on your need)
– Israel Merljak
I get it. I wanted to use only the basic operations, but I will consider using it as a solution. Thank you very much!
– Lucas
Interesting reading about the transformation to binary: https://answall.com/a/267825/64969; supposedly this answer should be very basic and explained, only needing to understand recursion.
– Jefferson Quesado