# Decimal to Binary and Binary to Decimal Program in C

Conversion from decimal to binary and vice-versa is a very important problem in computer science. We know that computer only understands binary numbers that are 0 and 1. Every data are given as input to the computer converts into a binary number system. In the same way, conversion of Binary to Decimal, Decimal to Octal and Octal to DecimalOctal to Binary and Binary to Octal also can be done.

## C Program to Convert Decimal to Binary

`````` #include<stdio.h>
int main()
{
int dec, bin=0, rem, i=1;

printf("Enter Decimal number: ");
scanf("%d",&dec);

while(dec!=0)
{
rem = dec % 2;
bin += (rem*i); // bin = bin + rem*i
dec /= 2; //dec = dec / 2
i *= 10; //i=i*10
}

printf("Binary= %d\n",bin);

return 0;
}``````

Output:-

Enter Decimal Number: 12
Binary Value= 1100

### Using the function

A function is a block of code that performs a specific task. For example, the main is a function and every program execution starts from the main function in C programming.

The C program is made of one or more functions. Every program must have at least one function with the name `main`. The execution of the program always starts from the main function and ends with the main function. The main function can call other functions to do some special task.

The function is a small program is used to do a particular task. In C a big program divided into several small subroutines/functions/procedures. Hence C is a function-oriented programming language.

`````` #include<stdio.h>
int convertToBinary(int n);
int main()
{
int num, binary;

printf("Enter Decimal Number: ");
scanf("%d",&num);

binary = convertToBinary(num);

printf("Binary Value= %d\n",binary);

return 0;
}

int convertToBinary(int n)
{
int rem, bin=0, i=1;

while(n!=0)
{
rem= n%2;
bin += (rem*i);
n /= 2;
i *= 10;
}

return bin;
}``````

## C program to convert binary to decimal

This time we need to calculate power. So, we will use pow() function defined under standard library function math.h

`````` #include<stdio.h>
#include<math.h>
int main()
{
int bin, dec=0, rem, i=0;

printf("Enter Binary number: ");
scanf("%d",&bin);

while(bin!=0)
{
rem = bin % 10;
dec += (rem * pow(2,i));
bin /= 10;
i++;
}

printf("Decimal = %d ",dec);

return 0;
}``````

Output:-

Enter binary number: 11101
Decimal value=29

If you are using Linux/Unix OS and getting an error then read it “undefined reference to sqrt (or other mathematical functions)” even include math.h header

### Using Function

`````` #include<stdio.h>
#include<math.h>
int convert(int bin);
int main()
{
int binary, decimal;

printf("Enter binary number: ");
scanf("%d",&binary);

decimal = convert(binary);

printf("Decimal value=%d",decimal);

return 0;
}

int convert(int bin)
{
int rem, i=0, dec=0;

while(bin!=0)
{
rem = bin%10;
dec += (rem*pow(2,i));
bin /= 10;
i++;
}

return dec;
}``````

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Similar C programming examples