Here is a C program to convert a binary number(base 2) to decimal number(base 10). Decimal number system is a base 10 number system using digits for 0 to 9 whereas binary number system is base 2 and uses 0 and 1. Given a decimal number as input from user we have to print the binary equivalent of input number.

##### For Example

100 in Decimal is equivalent to 1100100 in Binary number system.**Algorithm to convert Decimal to Binary number**

- Divide the input decimal number by 2 and store the remainder.
- Store the quotient back to the input number variable.
- Repeat this process till quotient becomes zero.
- Equivalent binary number will be the remainders in above process in reverse order.

##### For Example

Suppose input decimal number is 13

Step 1. 13/2 , Remainder = 1, Quotient = 6

Step 2. 6/2 , Remainder = 0, Quotient = 3

Step 3. 3/2 , Remainder = 1, Quotient = 1

Step 4. 1/2 , Remainder = 1, Quotient = 0

Now, the Binary equivalent of 13 is the remainders in reverse order : 1101

## C program to convert decimal number to binary

#include <stdio.h> long decimalToBinary(long n); int main() { long decimal; printf("Enter a decimal number\n"); scanf("%ld", &decimal); printf("Binary number of %ld is %ld", decimal, decimalToBinary(decimal)); return 0; } long decimalToBinary(long n) { int remainder; long binary = 0, i = 1; while(n != 0) { remainder = n%2; n = n/2; binary= binary + (remainder*i); i = i*10; } return binary; }Output

Enter a decimal number 25 Binary number of 25 is 11001

Enter a decimal number 64 Binary number of 64 is 1000000

## C program to convert binary number to decimal

#include <stdio.h> #include <math.h> long binaryToDecimal(long n); int main() { long binary; printf("Enter a binary number\n"); scanf("%ld", &binary); printf("Decimal number of %ld is %ld", binary, binaryToDecimal(binary)); return 0; } long binaryToDecimal(long n) { int remainder; long decimal = 0, i=0; while(n != 0) { remainder = n%10; n = n/10; decimal = decimal + (remainder*pow(2,i)); ++i; } return decimal; }Output

Enter a binary number 11001 Decimal number of 11001 is 25

**Related Topics**