C Program to Convert Binary Number to Decimal

Binary number system is 2-based which means at most two symbols, 0 (zero) and 1 (one), can be used to represent a binary number. Example of binary number is 110110011, sometimes binary numbers are written as 1101100112, where 2 is represented as base of the number system. Binary number system is the basis of all computer and digital systems. All information in computer and digital system are internally represented as binary number.

In contrast, decimal number system is 10-based that means at most 10 symbols (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) can be used to represent a decimal number. Example of decimal number is 435, sometimes written as 43510 which is same as binary 1101100112. Most of the time we use binary number system.

Binary Number to decimal conversion

There is common rule for conversion from any number system to decimal. Mathematical description of the rule is:

Numberb = (dN…d3d2d1d0)b = (dNxbN + … + d3xb3 + d2xb2 + d1xb1 + d0xb0)10

Where,
Numberb is the number of a number system with base b.
dN is the digit of N-th position starting from right.

This expression might appear too complex. If we can have one example, this will look easier. We’ll see how we can get 43510 from 1101100112.

Here our number id 1101100112, base is 2. The number has 9 digits. So, d8 = 1, d7 = 1, d6 = 0, d5 = 1, d4 = 1, d3 = 0, d2 = 0, d1 = 1, d0 = 1.

So the decimal equivalent is (1.28 + 1.27 + 0.26 + 1.25 + 1.24 + 0.23 + 0.22 + 1.21 + 1.20)10 = (256 + 128 + 0 + 32 + 16 + 0 + 0 + 2 + 1)10 = 43510.

C Program

C program that takes a binary number as input and prints the equivalent decimal number.

#include <stdio.h>

void main()
{
    unsigned long bin;
    unsigned ling decimal = 0;

    int i = 1, r;

    printf("Enter binary: ");
    scanf("%lu", &bin);

    while(bin)
    {
        r = bin % 10;
        if (r > 1)
        {
            printf("Not binary!\n");
            return;
        }
        decimal += r * i;

        i *= 2;
        bin = bin / 10;
    }

    printf("%lu in decimal.\n", decimal);
}

Outout:

Enter binary: 101001
41 in decimal.

Explanation:
Logic of the program is exactly what is explained in the previous section. The program takes the binary number as user input and saves that in an unsigned long integer bin. The while loop iterates as many time as the number of digits in the input binary nummber. If the input number is 101001 then the while loop will iterate 6 times. How? consider the line bin = bin / 10. It will remove last digit in every iteration from bin. After first iteration bin will become 10100 and in next iteration it will become 1010 and after 6 iterations it will become 0. The the while(bin) condition will break. The variable decimal starts with 0 and in every iteration (say iteration number n, starting with 0) dn.2n will be added. Because i starts with 1 (20), in next iteration it will become 2 (21), then 4 (22) and so on. Similarly, r will be the last digit of the input in first iteration, second last in second iteration, third last in third iteration and so on.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

0
0
0
0
0
1