# Thread: I need help with decimal to binary Algorithm

1. ## I need help with decimal to binary Algorithm

Hi there have been working on a project where I am asked to create a program that can accept input and give output.

I need to be able to reconise input as binary or decimal or error..

If input is Decimal then output is Binary equivalent.
If input is Binary then output is Decimal equivalent.

I have managed everything Up till the Decimal to Binary.. I Know how to do this on paper however I am at a point where I just cannot get the correct answer for this conversion.
Any Suggestions or nudge in the right direction would be greatly appreciated..

Here is my code so far... sorry its a little messy ill get round to cleaning it up after i have figured out this issue.. lol

Code:
```#include <stdio.h>
#include <string.h>

int main() {

char s;
char i;
char type=0;
int len;
int two;
int counter=1 ;
int sum = 0;
int index=0;

printf ("Type in a number: ");
gets (s);
if (s == '0'){
type = 'b';
} else if (s>='2' && s<='9'){
type = 'd';
} else if (s == '1') {
type = '\0';
} else {
type = 'i';
}

if (type != 'i') {
while (s[counter]!='\0'){
if (s[counter]>='2' && s[counter]<='9'){
type = 'd';
}else {
if (s[counter]!='0' && s[counter]!='1') {
type = 'i';
break;
} else {
if (type != 'b') {
type = '\0';
}
}
}

if (type=='d' && s=='0'){
type = 'i';
break;
}
counter++;
}

}if (type=='\0'){
printf ("is this number decimal (d) or binary (b)");
gets (i);
type = i;
}
// print number into variable, if n > 255 or n < 0 type == 'i'

if(type=='b') {
len = strlen (s)-1;
two = 1;

while (len >= 0) {
if (s[len] == '1') {
sum += two;
}
two = two*2;
len--;
}
printf("Converting binary to decimal. Answer is: %d\n", sum);
} else if(type=='d') {
/*this is where the conversion for decimal to binary will happen..
when figured out... lol  */
}

printf("number: %d", sum);

} else if(type=='i') {
printf("That is an invalid number!");
}

return 0;
}```

Again any help would be appreciated.

regards 2. 1. do not use gets - read FAQ
2. make small functions dedicated to one purpose like
int decimal2binary(int src, char* output, size_t bufLen);

Code:
```int main(void)
{
/* going to ask type of conversion */

switch(choice)
{
case 0: /* decimal2binary */
{
char buffer;
int src = getDecNumber();
/* perform conversion */
int res = decimal2binary(src,buffer,sizeof buffer);
if(res == 0) /* success */
{
/* output result */

etc```
this way you can post only the part of the code that is problematic and ask concrete question.
"Doesn't work" is too general 3. ## I was asking for help with the conversion of

decimal to binary.. I have left a comment in the code as to the area i am wanting to do it.

Regarding gets() sorry i didnt explain but that part was required as outlined in the brief i am using.

thanks for the response! will take all your points on board! 4. I have left a comment in the code as to the area i am wanting to do it.
The problem with your outline - it is very hard to find what is the input, what is the input format, and what should be the output

When you break code in functions - you can describe exactly what is the input parameter (with type and example of format) and what is the expected output 5. Your code is rather messy to read and the use of that gets function further complicates things. Popular pages Recent additions 