TechArena Community c program to convert decimal to binary
 User Name Remember Me? Password

#1
25-11-2009
 Member Join Date: Nov 2009 Posts: 65
c program to convert decimal to binary

I am studying inF.Y.B.Sc. I.T.and I got a assignment from college to create a program using C language that converts decimal number into binary number. I think it is very troublesome so suggest any function that convert decimal number into binary number. If there is no function then give me code.
#2
25-11-2009
 Member Join Date: Apr 2008 Posts: 1,948
Re: c program to convert decimal to binary

code to convert decimal number to binary number

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

void dec_bin(int num);

int main(void)
{
int m = 0;

printf("Digit (0-255): ");
scanf("%d", &m);

(m >= 0) && (m < 256) ? dec_bin(m) : exit(1);

return 0;
}

void dec_bin(int num)
{
int x, y;
x = y = 0;

for(y = 7; y >= 0; y--)
{
x = num / (1 << y);
num = num - x * (1 << y);
printf("%d", x);
}

printf("\n");
}```
#3
25-11-2009
 Member Join Date: May 2008 Posts: 2,008
Re: c program to convert decimal to binary

Code:
```#include <stdio.h>
void dec2bin(long deci, char *bin);
int main()
{
long dec;
char bin[80];
printf("\n\n Enter an integer value : ");
scanf("%ld",&deci);
dec2bin(deci,bin);
printf("\n The binary value of %ld is %s \n",deci,bin);
getchar();
getchar();
return 0;
}

void dec2bin(long deci, char *bin)
{
int k = 0, n = 0;
int neg_flag = 0;
int remain;
int old_deci;
char temp[80];

if (deci < 0)
{
deci = -deci;
neg_flag = 1;
}

do
{
old_deci = deci;
remain = deci % 2;
deci = deci / 2;
printf("%d/2 = %d remainder = %d\n", old_deci, deci, remain);
temp[k++] = remain + '0';
} while (deci > 0);

if (neg_flag)
temp[k++] = '-';
else
temp[k++] = ' ';

while (k >= 0)
{
bin[n++] = temp[--k];
bin[n-1] = 0;
}
}```
#4
25-11-2009
 Member Join Date: Apr 2008 Posts: 2,001
Re: c program to convert decimal to binary

Code:
```#include<stdio.h>
#include<conio.h>
void showbits(int m)
{
if(m==1)
printf("%d",m);
else
{
showbits(m/2);
printf("%d",m%2);
}
}
void main()
{
int n;
void showbits(int m);
clrscr();
printf("Number?");
scanf("%d",&n);
printf("\nBin eq of %d is ",n);
showbits(n);
getch();
}```

 Tags: