# C Program to Convert Decimal to Binary Number

In this tutorial, i am going to show you how to convert decimal numbers to binary numbers in c program with the help of for loop, while loop, function, and recursion.

## All C Programs to Convert Decimal to Binary Number

• C Program to Convert Decimal to Binary Number using While Loop
• C Program to Convert Decimal to Binary Number using For Loop
• C Program to Convert Decimal to Binary Number using Function
• C Program to Convert Decimal to Binary Number using Recursion

### C Program to Convert Decimal to Binary Number using While Loop

```/* C Program to Convert Decimal to Binary */

#include <stdio.h>

int main()
{
int a[10], number, i = 1, j;
printf("\n Please Enter Decimal Number  :  ");
scanf("%d", &number);

while(number  != 0)
{
a[i++] = number % 2;
number = number / 2;
}

printf("\n Binary Number of a Given Number =  ");
for(j = i - 1; j > 0; j--)  {
printf(" %d ", a[j]);
}
return 0;
}```

The result of the above c program; as follows:

```Please Enter Decimal Number  :  220
Binary Number of a Given Number =   1  1  0  1  1  1  0  0 ```

### C Program to Convert Decimal to Binary Number using For Loop

```/* C Program to Convert Decimal to Binary */

#include <stdio.h>
int main()
{
int a[10], number, i, j;
printf("\n Please Enter decimal number  :  ");
scanf("%d", &number);

for(i = 0; number > 0; i++)
{
a[i] = number % 2;
number = number / 2;
}

printf("\n Binary Number of a Given Number =  ");
for(j = i - 1; j >= 0; j--)  {
printf(" %d ", a[j]);
}
printf("\n");
return 0;
}```

The result of the above c program; as follows:

```Please Enter decimal number  :  100
Binary Number of a Given Number =   1  1  0  0  1  0  0 ```

### C Program to Convert Decimal to Binary Number using Function

```#include <stdio.h>
#include <math.h>
long decimalToBinary(int decimalnum)
{
long binarynum = 0;
int rem, temp = 1;
while (decimalnum!=0)
{
rem = decimalnum%2;
decimalnum = decimalnum / 2;
binarynum = binarynum + rem*temp;
temp = temp * 10;
}
return binarynum;
}
int main()
{
int decimalnum;
printf("Enter a Decimal Number: ");
scanf("%d", &decimalnum);
printf("Equivalent Binary Number is: %ld", decimalToBinary(decimalnum));
return 0;
}```

The result of the above c program; as follows:

```Enter a Decimal Number: 100
Equivalent Binary Number is: 1100100```

### C Program to Convert Decimal to Binary Number using Recursion

```#include <stdio.h>

int decimal_binary(int n)
{
if (n==0)
return 0;
else
return ((n%2)+10*decimal_binary(n/2));
}

void main()
{
int no;

printf("Enter a decimal number : ");
scanf("%d",&no);
printf("Decimal(%d) = Binary(%d)\n",no,decimal_binary(no));
}```

The result of the above c program; as follows:

```Enter a decimal number : 100
Decimal(100) = Binary(1100100)```

Categories C