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.

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 PointLeast Significant Bit (LSB)
22 21202-12-2 2-3
4210.50.250.125
Binary to decimal conversion table

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 number
Input:
11001010
Therefore, using positional notation we have-
= (11001010)2 
= 1x27+1x26+0x25+0x24+1x23+0x22+1x21+0x20 
= 128+64+0+0+8+0+2+0

Output:
202
Example:  Convert binary number 1100.1010 into decimal number
Input:
1100.1010
Therefore, using positional notation we have-
= (1100.1010)2 
= 1x23+1x22+0x21+0x20+1x2-1+0x2-2+1x2-3+0x2-4 
= 8+4+0+0+0.5+0+0.125+0

Output:
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 method

Input:
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 = 1

Again, 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 = 3
0 → 3 x 2 + 0 = 6
0 → 6 x 2 + 0 = 12
1 → 12 x 2 + 1 = 25
0 → 25 x 2 + 0 = 50
1 → 50 x 2 + 1 = 101
0 → 101 x 2 + 0 = 202

Output:
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.

  1. Initially, the program will prompt the user to enter any binary number and later store the value in long num.
  2. The following step is to call int convert(long num), wherein the input binary number will be passed.
  3. 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.

Output
Enter 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.


Recommended Books


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!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index