1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Program not giving a fully correct output :s

  1. Oct 15, 2012 #1
    Hi guys~

    I'm currently learning C and we've been asked to write a code where a decimal-system number gets converted to its corresponding binary form BUT, you know, when we do it manually by the 2-division method how the order of the binary numbers go from down to up...? yeah that is the part we were told to ignore for now because it requires us to learn another lesson.

    So, we were just told to give the output in the reverse form for now for example: 245 in binary is 11110101 but we were just told to give it in the reverse order for now like: 10101111...anyways, i kind of did it but for some reason it doesn't give me the last digit for any input: for 245 for instance, it only gives me 1010111 and excludes the last 1 there.

    Here's my code:

    Code (Text):

    #include <stdio.h>
    void main()
    {
        int a, mod;
        scanf("%d",&a);

        while(a/2!=0)
       {
            mod=a%2;
             printf("%d",mod);
              a=a/2;

       }
    }
     
    I'd like to know why this is happening...when i walk through the code, everything seems fine but what's with the last digit being excluded?
     
    Last edited by a moderator: Oct 15, 2012
  2. jcsd
  3. Oct 15, 2012 #2
    okkkk sorry! i got my answer, this thread can be deleted!!
     
  4. Oct 15, 2012 #3

    berkeman

    User Avatar

    Staff: Mentor

    No need to delete the thread. BTW, I added code tags around your code in your post, to improve the readability. Please try to use them in your future code posts. You can click "Quote" on your post, to see how they are used.

    :smile:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook