| | |
i need code for binary to decimal (it should be written in 'C')
![]() |
•
•
Join Date: Dec 2004
Posts: 3
Reputation:
Solved Threads: 0
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.
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) 

C Syntax (Toggle Plain Text)
/* 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; }
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
First, when you post some code, use code tags:
http://www.daniweb.com/techtalkforum...nouncementid=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:
http://www.daniweb.com/techtalkforum...nouncementid=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:
C Syntax (Toggle Plain Text)
#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; }
•
•
•
•
Originally Posted by fakhre
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.
C Syntax (Toggle Plain Text)
#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 */
here`s the code:
C Syntax (Toggle Plain Text)
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; }
Last edited by alc6379; Dec 17th, 2004 at 11:20 am.
"Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
![]() |
Other Threads in the C Forum
- Previous Thread: Color your console text
- Next Thread: Download 100% free Programming tutorials
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o ide inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft mysql number oddnumber open opendocumentformat openwebfoundation pdf pointer posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions systemcall test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






