11119 - binary addition
raw download clone
C
views 22
,
size 742 b
#include <stdio.h>

int main(void)
{
    int n, i = -1, j = 0, c = 0;
    int b[10] = {0};

    scanf("%d", &n);
    if(n == 0)
        printf("1 0");
    else
    {
        while(n != 0)
        {
            i++;
            b[i] = n % 2;
            n /= 2;
        }

        while(b[j] != 0)
        {
            b[j] = 0;
            c++;
            j++;
        }
        b[j] = 1;
        if(i >= j)
        {
            for(int k=i; k>=0; k--)
                printf("%d", b[k]);
            printf(" %d", c);
        }
        else if(i < j)
        {
            for(int k=j; k>=0; k--)
                printf("%d", b[k]);
            printf(" %d", c);
        }
    }

    return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.