# C Program to convert a number from binary to decimal

|In this C Programming example, we will implement the program to convert a number from binary to decimal and print the output on the console.

Table of Contents

## 1. How to convert binary to decimal in C?

**Binary** is the simplest kind of number system that accepts only two digits of 0 and 1 (i.e. the value of base 2). Whereas the Decimal number is the most common number system which accepts the base value as 10.

Let’s discuss two main approaches to convert a binary number into a decimal number.

### 1.1. Positional Notation method to convert binary to decimal

- Since numbers are the type of positional number system. That means the weight of the positions from right to left are-
**20, 21, 22, 23**… and so on for the integer part and the weight of the positions from left to right are-**2-1, 2-2, 2-3, 2-4**. - Consider any unsigned binary number is
**bnb(n-1) ….. b1b0.b-1b-2 …….b(m-1)bm**. Then the decimal number is equal to the sum of binary digits (**bn**) times their power of**2 (2n)**.

Most Significant Bit (MSB) | Binary Point | Least Significant Bit (LSB) | |||
---|---|---|---|---|---|

2^{2} | 2^{1} | 2^{0} | 2^{-1} | 2^{-2} | 2^{-3} |

4 | 2 | 1 | 0.5 | 0.25 | 0.125 |

In the following program, based on the user’s input the number entered is converted from binary to a decimal number using a ** user-defined function**.

Example:Convert binary number 11001010 into decimal numberInput:11001010 Therefore, using positional notation we have- = (11001010)_{2}= 1x2^{7}+1x2^{6}+0x2^{5}+0x2^{4}+1x2^{3}+0x2^{2}+1x2^{1}+0x2^{0}= 128+64+0+0+8+0+2+0Output:202

Example:Convert binary number 1100.1010 into decimal numberInput:1100.1010 Therefore, using positional notation we have- = (1100.1010)_{2}= 1x2^{3}+1x2^{2}+0x2^{1}+0x2^{0}+1x2^{-1}+0x2^{-2}+1x2^{-3}+0x2^{-4}= 8+4+0+0+0.5+0+0.125+0Output:12.625

### 1.2. Doubling method to convert binary to decimal

This is a simple approach to convert a binary number into a decimal number.

- Initially, start from the leftmost digit (or MSB) from the input.
- Take the most significant bit (MSB) and then multiply it by 2 and add a second leftmost bit, store it as a current result.
- Now, repeat and multiply by 2 with the current result and add the third leftmost bit.
- Update this value and follow this till the summation of the least significant bit (LSB or rightmost bit).

Since you are doubling (multiplying by 2) each time, so this method is known as **Doubling**.

Example:Convert binary number 11001010 into decimal number using Doubling methodInput:11001010 Therefore, using doubling method we have- Begin with "0 x 2" for the first digit and then add the resultant value with first digit. 11001010 1 → 0 x 2 + 1 =1Again, begin to multiply the resultant value with 2 and add the obtained value with next binary digit. Here next digit is 1, so 1 → 1 x 2 + 1 =30 → 3 x 2 + 0 =60 → 6 x 2 + 0 =121 → 12 x 2 + 1 =250 → 25 x 2 + 0 =501 → 50 x 2 + 1 =1010 → 101 x 2 + 0 =202Output:202

Helpful topics to understand this program better are-

## 2. C Program to convert a number from binary to decimal

Let’s address the execution(kind of pseudocode) for the program to convert a number from binary to decimal in C.

- Initially, the program will prompt the user to enter any binary number and later store the value in
`long num`

. - The following step is to call
`int convert(long num)`

, wherein the input binary number will be passed. `int convert(long num)`

coverts binary to decimal returns the decimal value and is finally printed on the console.

Let us now implement the above execution of the program *to convert a number from binary to decimal in C.*

#include <math.h> #include <stdio.h> int convert(long num); int convert(long num) { int decimal = 0, i = 0, rem; while (num != 0) { rem = num % 10; num /= 10; decimal += rem * pow(2, i); ++i; } return decimal; } int main() { long num; printf("Enter any binary number: "); scanf("%ld", &num); printf("%ld in binary is %d in decimal", num, convert(num)); return 0; }

**Note:** long data type is used to input long string from the user we can also use int, float, double datatype by declaring its format specifier as used above.

OutputEnter any binary number: 1100110001 1100110001 in binary is 817 in decimal

## 3. Conclusion

In this C Programming example, we have discussed *how to convert a number from binary to decimal* *in C*.

**Helpful Links**

Please follow **C Programming tutorials** or the **menu in the sidebar** for the complete tutorial series.

Also for the example C programs please refer to **C Programming Examples**.

All examples are hosted on **Github**.

**An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding**

**Do not forget to share and Subscribe.**

* Happy coding!! *😊