# Thread: Printing with all the 0s

1. ## Printing with all the 0s

Hello

I wrote this program to convert integer to binary and it works

Code:
```  while (decimal != 0) {
printf ("%d",decimal % 2);
decimal = decimal /2;
}```

How can I modify this that it prints something like "0000 0000 0000 0000"

for example when the user inputs 2 the program would print: "0000 0000 0000 0010"

I'v spend three days to figure this out, but I couldn't, Any help is appreciated.

Thanks

2. Your code prints out the 1's digit, then the 2's, then the 4's, etc. This is backwards. So you could store the elements in an array, then finally print them in reverse order. Alternatively, you can write a recursive function to do it - it's fairly simple, so I don't want to give away the whole thing.

3. yea, but I can't use arrays for this, this is part of a big assignment, all the other parts are done, but this is the part I'm stock on, and the bad thing is that Arrays can't be used.

4. The recursive function solution doesn't require arrays.

5. Originally Posted by robatino
The recursive function solution doesn't require arrays.

can you provide any hints please?

6. Originally Posted by arya6000
can you provide any hints please?
The only thing I'm willing to say is that the function body can be written in several lines (or less, depending on how you indent).

7. I should mention that what I'm thinking of only prints as many digits as it needs - for example, for 2, it would print 10, not 0000000000000010. If you need it to print exactly 16 digits, that would be more difficult, but could probably be done in a similar way. If you need it to print 16 digits in groups of 4, with spaces in between, as you wrote, that's a little tricky. If you know about the correspondence between hexadecimal and binary digits, that helps.

Edit: There's also an ugly solution involving bitwise operators, if you need the answer formatted exactly as you wrote.

8. Originally Posted by robatino
I should mention that what I'm thinking of only prints as many digits as it needs - for example, for 2, it would print 10, not 0000000000000010. If you need it to print exactly 16 digits, that would be more difficult, but could probably be done in a similar way. If you need it to print 16 digits in groups of 4, with spaces in between, as you wrote, that's a little tricky. If you know about the correspondence between hexadecimal and binary digits, that helps.

Edit: There's also an ugly solution involving bitwise operators, if you need the answer formatted exactly as you wrote.
yea thats what I'm trying to do, I have it working as a function. but I don't know how to print it like: "0000000000000010"

Edit: I meant to say "0000 0000 0000 0010" with spaces for each 4 binary digits

9. I just found out that the program I posted doesn't work correctly when I input the integer "100" it says the binary is "1"

What is a simple program that converts Integer to Binary?