Program not giving a fully correct output :s

  • Thread starter Thread starter Toyona10
  • Start date Start date
  • Tags Tags
    Output Program
Click For Summary
SUMMARY

The discussion centers on a C programming issue where a user attempts to convert a decimal number to its binary representation but encounters a problem where the last binary digit is omitted. The user’s code uses a while loop to divide the number by 2 and print the modulus, but it fails to include the final digit due to the loop condition. The solution involves modifying the loop to ensure the last digit is processed correctly.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with basic control flow statements in C
  • Knowledge of binary number representation
  • Experience with input/output functions in C, specifically scanf and printf
NEXT STEPS
  • Review C programming loops and conditions to understand flow control
  • Learn about binary number conversion techniques in programming
  • Explore debugging techniques in C to identify and fix code issues
  • Study the use of functions in C to modularize code for better readability
USEFUL FOR

Beginner C programmers, students learning about number systems, and anyone interested in improving their debugging skills in C programming.

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 ·
Replies
1
Views
11K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K