# C program to convert decimal to binary number

## C program to convert decimal to binary number

This page has a C program to convert decimal to binary number. We can write the C program to convert the given mixed decimal number into its equivalent binary. For Integer part conversiondivide the given integer part of the decimal number by 2 again and again until the number can be divided by 2 and then print the remainders of each division form bottom to top to form the binary equivalent. For decimal part conversion, multiply the given decimal part (number after the decimal point) of the decimal number by 2 again and again for atleast 5 or 7 steps(times) and then print the carries of each multiplication form top to bottom to form the binary equivalent.  The below given C program to convert a given mixed decimal number into its equivalent binary numberKindly check out the program to convert decimal to binary number.

### Convert decimal to binary

#include<stdio.h>
#include<string.h>
void decimal1_to_binary(long int);
void decimal2_to_binary(long int,int);
int power(int,int);
int main(){ int a=0,j=0,m,n=0,q=1;
long int k1,k2;
char g1[80], p1[80]={'\0'}, p2[80]={'\0'};
clrscr();
printf("Give a decimal number : ");
gets(g1);
for (a = 0; g1[a] != '\0'; a++)
{
q++;
}
for(m=0;m<q-1;m++){
if(g1[m] == '.')
break;
p1[m]=g1[m];
}
for(a=m+1;a<=q-1;a++){
p2[n]=g1[a];
n++;
j++;
}
k1= atoi(p1);
k2= atoi(p2);
printf("\n\n The given decimal number is : %s\n\n", g1);
decimal1_to_binary(k1);
for(m=0;m<q-1;m++){
if(g1[m] == '.'){
printf(".");
break;
}
}
if(g1[m] == '.')
decimal2_to_binary(k2, j-1);
getch();
return(0);
}

void decimal1_to_binary(long int s){
int b=1, h, remainde[40];
printf("\n\n The Binary number is : ");
while(s>0){
remainde[b] = s % 2;
s = s / 2;
b++;
}
b--;
for(h=b;h>=1;h--)
printf("%d", remainde[h]);
return(0);
}

void decimal2_to_binary(long int e, int u){
int a,p,r;
for(a=1;a<=8;++a){
e=e*2;
p=power(10,u);
if(e>=p){
r = e / p;
e = e % p;
printf("%d",r);
}
else
printf("%d",0);
}
return(0);
}

int power(int x,int n){
int i;
int numb = 1;
for (i = 0; i < n; ++i)
numb *= x;
return(numb);
}

Sample Output:

Enter an Integer decimal number : 13

The given decimal number is : 13

The Binary number is : 1101