converting decimal number to binaric

Viewed 65

I wrote a code that converts integer decimal number to binary. I'm trying to make it print all the number until the number given instead of printing the exact same number each time. for example, for input = 3 it will print 0,1,2,3 in binariy. this is my code:

printf("Please enter an integer number\n");
scanf_s("%d", &n);
for (i = 0; i < n; i++)
{
    printf("%d ==   ", n);

    for (c = 5; c >= 0; c--)
    {
        k = n >> c;

        if (k & 1)
            printf("1");
        else
            printf("0");
    }

    printf("\n");
}
system ("pause");
return 0;

thanks guys!

2 Answers

You need to work with i in the loop, not n

#include "stdio.h"



int main()
{
    int c,k,n,i = 0;
    printf("Please enter an integer number\n");
    scanf_s("%d", &n);
    for (i = 0; i < n; i++)
    {
        printf("%d ==   ", i);

        for (c = 5; c >= 0; c--)
        {
            k = i >> c;

            if (k & 1)
                printf("1");
            else
                printf("0");
        }

        printf("\n");
    }

    return 0;
}

You can also simply output the single characters with putchar(), no need to call the variadic printf to output a fixed character within the format string (though a good compiler would likely make the optimization for you.

You can do something similar to:

#include <stdio.h>
#include <limits.h>

int main (void) {

    unsigned n;
    fputs ("enter an integer number: ", stdout);
    if (scanf ("%u", &n) == 1) {
        for (unsigned i = 0; i < n; i++) {
            printf ("%d ==   ", i);
            for (unsigned s = 0; s < sizeof s * CHAR_BIT; s++)
                putchar ((i >> (31 - s)) & 1u ? '1' : '0');
            putchar ('\n');
        }
    }
}

Example Use/Output

$ ./bin/binslessthann
enter an integer number: 10
0 ==   00000000000000000000000000000000
1 ==   00000000000000000000000000000001
2 ==   00000000000000000000000000000010
3 ==   00000000000000000000000000000011
4 ==   00000000000000000000000000000100
5 ==   00000000000000000000000000000101
6 ==   00000000000000000000000000000110
7 ==   00000000000000000000000000000111
8 ==   00000000000000000000000000001000
9 ==   00000000000000000000000000001001

(of course there is no need for the full 32-bits for small numbers, but they were included for completeness)

Unpadded Binary Representation

As noted in the comments, an unpadded version makes the results much easier to read. (the quote was "horrible leading zeros") To output the unpadded version, you can simply check first if the value is zero, if so simply output zero, for values with ones-bits present, you check whether ones bits remain in the value after the shift, if so, continue outputting bits, otherwise don't output anything further, e.g.

int main (void) {

    unsigned n;
    fputs ("enter an integer number: ", stdout);
    if (scanf ("%u", &n) == 1) {
        for (unsigned i = 0; i < n; i++) {
            printf ("%d ==   ", i);
            if (!i)
                putchar ('0');
            else {
                unsigned s = sizeof s * CHAR_BIT;
                while (s--) {
                    unsigned remain = i >> s;
                    if (remain)
                        putchar ((i >> s) & 1u ? '1' : '0');
                }
            }
            putchar ('\n');
        }
    }
}

Example Use/Output

$ ./bin/binslessthanrem
enter an integer number: 10
0 ==   0
1 ==   1
2 ==   10
3 ==   11
4 ==   100
5 ==   101
6 ==   110
7 ==   111
8 ==   1000
9 ==   1001

Look things over and let me know if you have further questions.

Related