C Program to convert the binary number into decimal

April 1, 2022, Learn eTutorial

What are binary and decimal numbers?

In this C program, we have to convert the given Binary number into Decimal. Converting a Binary number into a Decimal using the C program is very simple. A Binary number is a number that is represented by only Zeros and Ones. So we need to convert these Zeros and Ones to a number characterized by a Zero to Nine Decimal system. Binary numbers have the Base of 2 where the Decimal numbers are of the base 10.

The Binary number system is essential in Computers, which can only read Binary numbers, called Machine Language. Binary numbers are represented by subscript 2, and Decimals are represented by subscript 10.

How to convert a binary to decimal?

In this c program, we are using a Mod operator inside a 'while loop' to convert the Binary into Decimal. First, we calculate the Remainder using the Mod, and we add 'dec' with Remainder * Base, the Base is initialized to 1. Divide the number by 10 to remove the last digit, and finally, we multiply Base with 2 to get the output. Let's take a simple example to understand more about this.

1. Let '0010' be the Binary number. Take the numbers from the right sides as the powers of 2, which means from the right.

      0 - 2^0
      1 - 2^1
      0 - 2^3 


Multiply each digit of the Binary number starting from the last with the powers of 2 respectively, Then add all the multiplied digits we get 2 as a decimal digit.

The main logic of this program is to Initialize the variable 'dec=0' and 'base=1'. Then read the Binary number into 'num' and copy it into 'bnum'. Using a 'while loop' with the condition 'num>0', calculate the Remainder by using the mod operator. Then find the Decimal by num=num/10 and base=base*2. Repeat the loop, and display the Binary number as bnum and decimal equivalent as dec.


STEP 1: Include the Header files to use the built-in functions in the C program.

STEP 2: Declare the Integer varables 'num', 'bnum', 'dec=0', 'base=1' and 'rem'.

STEP 3: Read a Binary number from the user and store it into bnum and num.

STEP 4: By using a 'while loop' with the condition num>0, do step 5

STEP 5: rem=num and find dec=dec+rem*base

STEP 6: num = num / 10 , base = base * 2;

STEP 7: Repeat step 4

STEP 8: Display the Binary number as bnum.

STEP 9: Display its Decimal equivalent number as 'dec'.

C Source Code

                                          #include <stdio.h>

void main() {
  int num, bnum, dec = 0, base = 1, rem;
  printf("Enter a binary number(1s and 0s)\n");
  scanf("%d", & num); /*maximum five digits */
  bnum = num;
  while (num > 0) {
    rem = num%10; /* calculating reminder using mod operator */
    dec = dec + rem * base;
    num = num / 10; /* finding the decimal  */
    base = base * 2;
  printf("The Binary number is = %d\n", bnum);
  printf("Its decimal equivalent is =%d\n", dec);
} /* End of main() */


Enter a binary number(1s and 0s)

The Binary number is = 10101

Its decimal equivalent is =21