Question:
Decimal to binary conversion in C?
Arunit
2012-05-15 22:32:16 UTC
include

main ()

{

int storage[50];
int input, result,updation=0,counter;
printf ("Please enter a decimal number: ");
scanf ("%d", &input);
for (counter=0;counter<50;counter++)
{
updation+=1;
result=input%2;
input=input/2;

storage[counter]=result;

if (input==0)
break;


if (updation == 50)
printf ("\nThe number is too large to convert...");
goto escape;
}
printf ("\n");
for (counter=updation-1;counter>=0;counter--)
{
printf ("%d", storage[counter]);
}
escape:

getch ();
}



Ive written this code... though everything seems ok but the code is not working.... can u tell me why??
Four answers:
PC
2012-05-15 22:53:57 UTC
First, do not. NOT. use goto. Yes, most programming languages will let you do it, but its considered one of the biggest sins a programmer can make.



include <--- you forgot the # here



main () <----- you need a return type, even if its void



if (updation == 50)

printf ("\nThe number is too large to convert...");

goto escape;

This part probably isn't working as you intend. Add brackets around the printf and goto statement (in fact, just get rid of the goto and use a break). The if statement is only affecting the printf in this block. The goto escape; line is always being run.



for (counter=updation-1;counter>=0;counter--… <-- i'm assuming the ... is supposed to be a closing paren



getch (); <----- you need to include the conio library to use this function
Jim
2012-05-15 23:27:42 UTC
- there's no way the data width is going to get to 50 bits. try 31 since you are using a *signed* integer insead of unsigned (in which case you can have 32). or better yet, why don't you try

unsigned long long

, so you can have 64?

- you forgot a close } on your first for statement

- you forgot to do a return 0; just before the end of main()

- avoid goto statements where possible. bad practice. try else.

- #include not include

- int main(void) or int main(int argc, char * argv[]) or int main(int argc, char * argv[], char * envp[]), but your syntax is wrong and should cause a problem.

- you should not break on 0, how are you going to display the rest of the digits?

- have to reverse the results... so you may need a string reversal function. your string.h library may have a strrev or _strrev in it. maybe. it's not a standard function. you will also need to convert those digits to chars. I suggest you use a char string.

- you should break; if number is too large to convert.

- I suggest you use #define for your number of bits NUMBITS like this: #define NUMBITS 64 or #define NUMBITS 50 and then use NUMBITS from there on as your replacement for 50. this way, if you make a mistake, you only need to change it in one place, and by the way, const or int or unsigned doesn't work for array declarations unless you are doing C++ and using the new operator.



#include //printf(), scanf()

#include //getch()

#include //strrev();

#define NUMBITS 64



int main (void)

{

char digitchars[] = "0123456789abcdef";

char storage[NUMBITS+1];

char * pstorage = storage;

unsigned long long input, inputcopy; //64-bit unsigned integer

unsigned bitposition, result; //can't use an unsigned longlong as an index

storage[ NUMBITS ] = '\0'; //needs null terminator

printf ("Please enter a decimal number: ");

scanf ("%I64u", &input);

inputcopy = input;

for (bitposition=0; bitposition < NUMBITS; bitposition++)

{

result=input%2;

input=input/2;



storage[bitposition] = digitchars[result];



/*if (input==0) {

break;

}*/

}

if (0 != input)

{

printf ("\nThe number is too large to convert...");

//actually, you will never get here, the nbumber isn't going to get bigger than the bit width of the data type it's stored in.

}

strrev(storage);

//search for first non-leading zero

while ('0' == *pstorage) {

pstorage++;

}

if ('\0' == *pstorage) {

//we hit end of string. must be all zeros. so point to last zero.

pstorage = storage + NUMBITS - 1;

}

printf("%I64u-->%s\n", inputcopy, pstorage);

getch();

return 0;

}
James Bond
2012-05-15 23:08:23 UTC
include

#include

int main (){

int storage[50], input, result,updation=0,counter;

printf ("Please enter a decimal number: ");

scanf ("%d", &input);

for (counter=0;counter<50;counter++){

updation+=1;

result=input%2;

input=input/2;

storage[counter]=result;

if (input==0)break;

}



if (updation == 50){

printf ("\nThe number is too large to convert...");

exit(-1);

}

printf ("\n");

for (counter=updation-1;counter>=0;counter--) printf ("%d", storage[counter]);

return 0;

}
clarice
2016-10-18 02:15:43 UTC
trouble-free exchange of all references to the huge sort 8 (which contain in the 1st cout assertion) to the huge sort sixteen. i do no longer think that it will actually convert a binary huge sort to a decimal huge sort, because of the fact it would not evaluate the area fee of the 'a million' digit. it may be this: if (b[i] == 'a million') sum += pow(2, sixteen - i); additionally, there is yet another enormous assumption. That the climate of the 'b' variable are already assigned to the NULL character. There must be a decision like this as we communicate after the "char b[8];": memset(b, 0, sixteen); And to extra simplify issues, it particularly is greater efficient to apply a macro or a relentless variable to define the size of the array and how long it loops, via making use of something like this: #define NUMDIGITS 8 or const int numdigits = 8; Then, the place you notice the huge sort 8 in the code, it may get replaced via the two "NUMDIGITS", or "numdigits", based upon which one you chosen to apply. And the 1st cout() assertion must be greater like this: cout << "Please enter a binary huge sort as much as " << NUMDIGITS << " digits...nn"; right this is the code with the adjustments I reported in place: #contain #contain #contain making use of namespace std; #define NUMDIGITS sixteen int important() {    char b[NUMDIGITS];    memset(&b, 0, NUMDIGITS);    cout << "Please enter a binary huge sort as much as " << NUMDIGITS << " digits...nn";    cin >> b;    int sum = 0, i=0;    together as (i < NUMDIGITS && b[i] != (char)0) { //each element of b[] is initialized to (char)0 - NUL       if (b[i] == 'a million') sum += pow(2, NUMDIGITS - i);       else if (b[i] == '0') ;       else cout << " you're no longer inputing a binary huge sort! n";       i++;    }    cout << sum;    char c; cin >> c;    return 0; } //end important


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...