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;
}
}
Bookmarks