Program not giving a fully correct output :s

  • Thread starter Thread starter Toyona10
  • Start date Start date
  • Tags Tags
    Output Program
AI Thread Summary
The discussion revolves around a C programming issue where a user is trying to convert a decimal number to binary but is encountering a problem where the last binary digit is not printed. The user initially shared their code, which uses a loop to divide the number by 2 and print the remainder. The issue arises because the loop condition prevents the last digit from being processed. The user later realizes the solution to their problem and expresses a desire to delete the thread, but another participant suggests keeping it for future reference and improves the code's readability. The thread highlights common challenges faced by beginners in programming and the importance of community support.
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:
 

Similar threads

Replies
1
Views
10K
Replies
3
Views
1K
Replies
5
Views
2K
Replies
9
Views
4K
Replies
1
Views
4K
Replies
12
Views
2K
Back
Top