hi ppl

i am fakhre, and i am new, not only at your web but also in 'C' language.
will any one of u will help me, i need a simple code written with while loop to convert binary to decimal.
your frnd
Fakhre.

Recommended Answers

im not sure what constraints you have on how to code it but heres what i came up with after a few minutes of head scratching. it has a for and do-while and even stores the binary number if you need if afterwards. Ideally you would want to put the …

Jump to Post

First, when you post some code, use code tags:
http://www.daniweb.com/techtalkforums/announcement.php?f=8&announcementid=3

2nd, why not use a newer compiler?

3rd, here is the code posted before by 1o0oBhP in C++; i simply put it in …

Jump to Post

well my effort is just like this

<snip code>

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. …

Jump to Post

All 16 Replies

ok thank you!

to say this ;)
well my effort is just like this
#include<conio.h>
#include<stdio.h>
int main()
{
int a,b,c,d,e,f,g,h,i,j;
clrscr();

printf("enter four binary numbers for decimal value=");
scanf("%d",&a);
b=a/10;
c=b/10;
d=c/10;
e=d/10;
f=a%10;
g=b%10;
h=c%10;
i=d%10;

if(i>0)
i=8;
else {
i=0; }

if(h>0)
h=4;
else {
h=0; }

if (g>0)
g=2;
else {
g=0; }

if(f>0)
f=1;
else{
f=0; }
e=e;
j=i+h+g+f;
printf("%d\n",j);
printf("%d%d%d%d",i,h,g,f);
getch();
return 0;
}

this code has been rejected by my teacher. i want to make with while loop.
so now plz help me.
thank you.
Fakhre.

im not sure what constraints you have on how to code it but heres what i came up with after a few minutes of head scratching. it has a for and do-while and even stores the binary number if you need if afterwards. Ideally you would want to put the conversion code in a function taking either a char[8] or bool[8] parameter and returning the result. Technically since i have used an 8 bit binary number the result should by a byte (unsigned char) :)

/* Binary to Decimal **********************************************************/

#include <iostream>
#include <cstdio>
using namespace std;

bool binary[8]; // unused, but if you need to store it, it will be in here
char str[8];    // input string
int result;     // integer result
int power = 1;  // power of 2. This is needed because of (2 ^ i) problem (see below)

int main(void)
{
    do  
    {
        cout << "Please input an 8 bit binary number or type q to quit:";
        cin >> str;
    
        result = 0; // reset to 0
        power = 1;  // first power = 2 ^ 0 = 1
        
        /* convert it */
        for(int i = 0; i < 8; i++)
        {
            if(str[i] == '1')
            {
                binary[i] = 1;// make binary number
                result += power; // modify result
            }    
            power *= 2; /* id like to use 2 ^ i instead of power but it
                         DOESNT give 1, 2, 4, 8, 16 ect... */
        }
        
        /* output */
        cout << "\nThe binary number " << str << " = " << result << "\n";
    } while (str[0] != 'q');   // do - while until "q" is typed 
    return 0;
}

well thank you for your support that you are giving me time, but i am sorry to say that problem is stil there, coz i need it for "borland c" and code should be written under #include<conio.h> and #include<stdio.h>.
well plz if you can help me in this case i will be very thankful of you...

Fakhre!

First, when you post some code, use code tags:
http://www.daniweb.com/techtalkforums/announcement.php?f=8&announcementid=3

2nd, why not use a newer compiler?

3rd, here is the code posted before by 1o0oBhP in C++; i simply put it in a C format:

#include<stdio.h>

char binary[8]; // unused, but if you need to store it, it will be in here
char str[8];    // input string
int result;     // integer result
int power = 1;  // power of 2. This is needed because of (2 ^ i) problem (see below)

int main(void)
{
    do  
    {
        printf ("Please input an 8 bit binary number or type q to quit:");
        scanf ("%s", str);
    
        result = 0; // reset to 0
        power = 1;  // first power = 2 ^ 0 = 1
        
        /* convert it */
        for(int i = 0; i < 8; i++)
        {
            if(str[i] == '1')
            {
                binary[i] = 1;// make binary number
                result += power; // modify result
            }    
            power *= 2; /* id like to use 2 ^ i instead of power but it
                         DOESNT give 1, 2, 4, 8, 16 ect... */
        }
        
        /* output */
        printf ("\nThe binary number %s = %d\n\n", str, result);
    } while (str[0] != 'q');   // do - while until "q" is typed 
    
    return 0;
}

well my effort is just like this

<snip code>

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 havent you just done DECIMAL to BINARY?? i thought we were doing BINARY to DECIMAL ???

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).

here`s the code:

int main()
{
clrscr();
int b,d=0,a[100];
for(int z = 0;z<100;z++)        //initialising
 {
  a[z] = 0;
 }

cout<<"enter the binary no."<<endl;
cin>>b;
int c = 0,p;
while(b>0)
{
 int r = b%10;
 b = b/10;
 p = pow(2,c);
 d = d + (r*p);
 a[c] = r;
 c++;
}
cout<<"the decimal equivalent is "<<d<<endl;
return 0;
}

here`s the code:

cout<<"enter the binary no."<<endl;
cin>>b;
cout<<"the decimal equivalent is "<<d<<endl;

My, what strange C you write. And use code tags: [code][/code]

hi ppl

i am fakhre, and i am new, not only at your web but also in 'C' language.
will any one of u will help me, i need a simple code written with while loop to convert binary to decimal.
your frnd
Fakhre.

void main()
{
int a,b,c;
printf("enter intger number");
scanf("%d",&a);
while(a>0)
{ b=a%2;

a=a/10;
printf("%d",a);
}
getch();
}

differnce in stdio.h and conio.h in c language

seems like a lot of solutions here. hope that they are useful fakhre, i tested mine a lot and still no errors but i didnt know about the pow function, it could simplify mine a bit (remove power, change 2^i with pow(2, i) i think) :)

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...

>Argh! Why must you people fill my brain with hurting!?
>Too much bad code

:sad: :cry:

Be a part of the DaniWeb community

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