# Decimal to binary conversion by C

We can convert decimal to binary using C program. In this article we will convert a decimal number to its equivalent binary number through a C program.

We know the base of decimal number is 10 and the base of binary number is 2. Here we will perform this program assuming that the size of integer is 4 byte (32 bits). We will use bitwise operator here to make this program. So, lets see the program to convert a decimal number to its equivalent binary number.

## Decimal to binary using bitwise operator in C

``````#include <stdio.h>
int main(){
int num, i, j;

printf("Enter a decimal number to convert : ");
scanf("%d", &num);

printf("\nBinary equivalent of %d is = \n", num);

for (i = 31; i >= 0; i--){
j = num >> i;

if (j & 1){
printf("1");
}else{
printf("0");
}
}

printf("\n");

return 0;
}``````

### Output of this decimal to binary program: ## Decimal to binary using array in C

``//  we are updating``

### Output of this decimal to binary program ## Convert decimal to binary using recursion

``````#include <stdio.h>
#include <stdlib.h>

char *converter(int);

int main(){
int num;
char *a;

printf("Enter a number to convert : ");
scanf("%d", &num);

a = converter(num);
printf("\nBinary equivalent of %d is = %s\n", num, a);

free(a);

return 0;
}

char *converter(int k){
int x, y, i;
char *z;

i = 0;
z = (char*)malloc(32+1);

if (z == NULL)
exit(EXIT_FAILURE);

for (x = 31 ; x >= 0 ; x--){
y = k >> x;

if (y & 1){
*(z + i) = 1 + '0';
}else{
*(z + i) = 0 + '0';
}
i++;
}
*(z + i) = '\0';

return  z;
}``````

### Program output is as follows:

``````Enter a number to convert : 63

Binary equivalent of 63 is = 00000000000000000000000000111111

Process returned 0 (0x0)   execution time : 14.774 s
Press any key to continue.``````

Previous pageReverse number

Next pageAssembly code in C

Recommended for you: