Convert decimal to binary in C

后端 未结 16 772
攒了一身酷
攒了一身酷 2020-12-10 09:38

I am trying to convert a decimal to binary such as 192 to 11000000. I just need some simple code to do this but the code I have so far doesn\'t work:

void de         


        
16条回答
  •  醉酒成梦
    2020-12-10 10:18

    This is the simplest way to do it

    #include 
    
    void main()
    {
        int n,i,j,sum=0;
        printf("Enter a Decimal number to convert it to binary : ");
        scanf("%d",&n);
        for(i=n,j=1;i>=1;j*=10,i/=2)
            sum+=(i%2)*j;
        printf("\n%d",sum);
    }
    

提交回复
热议问题