Program not giving a fully correct output :s

In summary, the conversation discusses a coding assignment where a decimal number is converted to binary form using the 2-division method. The focus is on giving the output in reverse order and the code provided by one participant is not giving the last digit. The issue is resolved and the thread does not need to be deleted. Code tags are suggested for future posts.
  • #1
Toyona10
31
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
  • #2
okkkk sorry! i got my answer, this thread can be deleted!
 
  • #3
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:
 

1. Why is my program not giving a fully correct output?

There could be several reasons for this. It could be due to an error in the code, incorrect input values, or a logical mistake in the algorithm. It is important to carefully review the code and identify the source of the issue.

2. How can I debug my program to find the error?

One way to debug your program is by using a debugger tool. This will allow you to step through your code and see the values of variables at each step, helping you identify where the error is occurring. You can also try printing out the values of variables at different points in the code to track their values.

3. Is it possible that the program is giving a partially correct output?

Yes, it is possible for a program to give a partially correct output. This could happen if the program was able to execute some parts of the code correctly, but encountered an error or mistake in another part. It is important to thoroughly test and review the code to ensure it is giving the desired output.

4. Can incorrect input values cause the program to not give a fully correct output?

Yes, incorrect input values can definitely affect the output of a program. If the program is expecting certain types of input or specific values, providing incorrect input can cause the program to produce unexpected results. It is important to check the input values and ensure they align with what the program is expecting.

5. What can I do to ensure my program gives a fully correct output?

To ensure that your program gives a fully correct output, it is essential to thoroughly test and review the code. It is also helpful to have a peer review your code to catch any mistakes or errors that may have been overlooked. Additionally, using good programming practices, such as writing clear and concise code and using comments, can help prevent errors and improve the overall quality of the program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
670
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
14
Views
2K
Back
Top