Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
well my effort is just like this
this code has been rejected by my teacher. i want to make with while loop.
so now plz help me.
Wherever there is repetition, there ought to be a loop. Notice the repeated statements %10 and /10. Also notice the pattern of doubling the place value multiplier.
#include <stdio.h>
int main(void)
{
int binary = 0; /* initialize the result to zero */
int bitvalue = 1; /* starting place value for bits */
int decimal = 1011; /* "decimal" value to "convert" */
while ( decimal )
{
binary += decimal % 10 * bitvalue; /* get new bit and add to result */
bitvalue *= 2; /* adjust base-2 place value */
decimal /= 10; /* get next "decimal" */
}
printf("binary = %d\n", binary);
return 0;
}
/* my output
binary = 11
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
dave havent you just done DECIMAL to BINARY?? i thought we were doing BINARY to DECIMAL ???
It looked to me like "decimal-coded binary" to decimal from the original attempt (hence "decimal" in the comments).
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
here`s the code:
cout<<"enter the binary no."<>b;
cout<<"the decimal equivalent is "<My, what strange C you write. And use code tags : [code][/code]
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
cscgal
The Queen of DaniWeb
19,422 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
Argh! Why must you people fill my brain with hurting!? And people ask me why I'm angry all of the time. I direct rage at anyone who insults the effort I put into learning how to use C properly.
Too much bad code, going somewhere else to cool off now...
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>Argh! Why must you people fill my brain with hurting!?
>Too much bad code
:sad: :cry:
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314