Hi guys. Hope all is well.
There's another concern too.
If one is to write a code in which a number would be changed into its binary equivalent, and the binary equivalent would be printed, how does one untie this knot?

#include<stdio.h>
main()
{
  int n;                                         /* the number */
 int b;                                          /* the binary part */

 printf(" Enter a number,plz :");
 scanf("%d", &n);

b= m%2                                     /* this is the operation required */

while( b!= 0)                               /* this is the part where things  */
{                                              /* become wozzy */

The loop has to continue until the b==0. How do I have the b @ every loop recorded for the final output? Or do I have to include this in the loop :

b = b%2 . I think I can include that in the loop (now that I think of it). But I still can't figure out a way to print all the b's @ every stage of the loop after the loop has ended.

Plz, do not give me the code. Just point me in the right direction.

Thx for the input.

Recommended Answers

All 6 Replies

Here is an code snippet with the code, but if you don't want to ruin the thinking I will try to give you an hint

Firstly: Take care of negative input (How does binary look when negative? Does the formule work on an negative number?)

Secondly: Maybe should you make sure that the loop occures at least once

Thirdly: You might have the binary The wrong way around when you get out of the loop, try to reverse it in an second loop

Int the loop itself: You should

//whittle down decimal

and

// makes characters 0 or 1


Lol, I'm kind of speaking in riddles. Hope it points you quite in the right direction, if not, just forget it and look into the snippet :P.

#include<stdio.h>
[B]int[/B] main() // main's return type is int
{
/* ... */
b= m%2; // I'm assuming you mean [B]n%2[/B]

while( b!= 0) // this will check if [B]n[/B] is even or odd. if it's even, the loop will be skipped.  See below.

The loop has to continue until the b==0.

You probably mean until n == 0. b is just the LSB of n, which could become 0 at any point during the operation. Also, if you've already done b = b%2 , doing it repeatedly will never change the value of b.

How do I have the b @ every loop recorded for the final output? Or do I have to include this in the loop :
b = b%2 .

You'll probably have this in the loop. See above about using n%2 vs. using b%2 (you'll probably want b = n % 2; ). And think about what you need to do to n as well.

But I still can't figure out a way to print all the b's @ every stage of the loop after the loop has ended.

Why not just print them inside the loop?

Thx for the reply, anony. I was trying to understand ur comment, and I thought your code might help, but it sure threw me farther away into oblivion.
I would try your reasoning though. I don't think the code shoul dbe that complex as you wrote it. The chapter I'm on has covered some of the things you wrote about, so I guess the assignment must be within what I have learnt.
Thanks very much for the input.

While (b not 0)
{
    test odd or even
    {
        save the result (0 or 1) in an array
    }
    shift b right 1 bit (or divide by 2)

}
output the array from last value to the first.

If you use the division version, b should be an unsigned value.

Alternate for the loop, if your value is 32 bits, simply use a for loop and you won't have to worry about signed/unsigned and your output would be a full 32 bits.

Hi guys. Hope all is well.

hile( b!= 0)                               /* this is the part where things  */
{                                          /* become wozzy */

Create a array variable (For eg:num[] and assing the b value with the loop and simultaneously incresa the Subscript value of the num and U can print it after U ended the Loop.

b = decimal;
while ( b != 0 )
{
   n = n * 10 + ( b % 2 );
   b /= 2;
}

while ( n != 0 )
{
   binary = binary * 10 + ( n % 10 );
   n /= 10;
}

The above is one of the method where you can change decimal number to binary. It depends on how to apply the logic into a programming language. The first loop above is to convert the decimal number to binary. However, the binary is in a reverse order. Therefore, you will need the second loop to reverse it and get the answer. You can try the mathematical calculation manually so that you can understand it more clearly. Hope it helps you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.