Thread: Printing with all the 0s

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    48

    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. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    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. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The recursive function solution doesn't require arrays.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    Quote Originally Posted by robatino View Post
    The recursive function solution doesn't require arrays.

    can you provide any hints please?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by arya6000 View Post
    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. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.
    Last edited by robatino; 11-01-2007 at 07:59 PM.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    Quote Originally Posted by robatino View Post
    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
    Last edited by arya6000; 11-01-2007 at 08:21 PM.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    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?

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  3. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM
  5. Printing
    By Dual-Catfish in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 05-25-2002, 08:10 PM