954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Loop challenge

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.

the.future.zone
Newbie Poster
15 posts since Aug 2006
Reputation Points: 23
Solved Threads: 1
 

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.

Anonymusius
Posting Whiz in Training
238 posts since Aug 2006
Reputation Points: 129
Solved Threads: 11
 
#include<stdio.h>
<strong>int</strong> main() // main's return type is int
{
/* ... */
b= m%2; // I'm assuming you mean <strong>n%2</strong>

while( b!= 0) // this will check if <strong>n</strong> 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?

Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
 

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.

the.future.zone
Newbie Poster
15 posts since Aug 2006
Reputation Points: 23
Solved Threads: 1
 
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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

[quote=the.future.zone;251678]Hi guys. Hope all is well.
hile( b!= 0) /* this is the part where things */
{ /* become wozzy */
[/code]



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.

thiyagu_mca2006
Newbie Poster
12 posts since Sep 2006
Reputation Points: 13
Solved Threads: 0
 
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.

rinoa04
Junior Poster in Training
84 posts since Sep 2006
Reputation Points: 52
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You