Program not giving a fully correct output :s

  • Thread starter Thread starter Toyona10
  • Start date Start date
  • Tags Tags
    Output Program
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
Toyona10
Messages
31
Reaction score
0
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:
#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:
Physics news on Phys.org
okkkk sorry! i got my answer, this thread can be deleted!
 
Toyona10 said:
okkkk sorry! i got my answer, this thread can be deleted!

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: