How to make a program to convert Binary into Decimal in C/C++

How to make a program to convert Binary into Decimal in C/C++

  • First of all, we are going to discuss the logic behind the conversion of Binary number into the Decimal number.
Conversion of binary to decimal (base-2 to base-10) numbers and back is an important concept to understand as the binary numbering system forms the basis for all computer and digital systems.
The decimal or “denary” counting system uses the Base-of-10 numbering system where each digit in a number takes on one of ten possible values, called “digits”, from 0 to 9, eg. 21310 (Two Hundred and Thirteen).
But as well as having 10 digits ( 0 through 9 ), the decimal numbering system also has the operations of addition ( + ), subtraction (  ), multiplication ( × ) and division ( ÷ ).
In a decimal system, each digit has a value ten times greater than its previous number and this decimal numbering system uses a set of symbols, b, together with a base, q, to determine the weight of each digit within a number. For example, the six in sixty has a lower weighting than the six in six hundred. Then in a binary numbering system, we need some way of converting Decimal to Binary as well as back from Binary to Decimal.
Any numbering system can be summarised by the following relationship:
  N = bqi
where:N is a real positive number
b is the digit
q is the base value
an integer (i) can be positive, negative or zero
N = bn qn… b3 q3 + b2 q2 + b1 q1 + b0 q0 + b-1 q-1 + b-2 q-2… etc.

The Decimal Numbering System

In the decimal, base-10 (den) or denary numbering system, each integer number column has values of units, tens, hundreds, thousands, etc as we move along the number from right to left. Mathematically these values are written as 100, 101, 102, 103 etc. Then each position to the left of the decimal point indicates an increased positive power of 10. Likewise, for fractional numbers, the weight of the number becomes more negative as we move from left to right, 10-1, 10-2, 10-3 etc.
So we can see that the “decimal numbering system” has a base of 10 or modulo-10(sometimes called MOD-10) with the position of each digit in the decimal system indicating the magnitude or weight of that digit as q is equal to “10” (0 through 9). For example, 20 (twenty) is the same as saying 2 x 101 and therefore 400 (four hundred) is the same as saying 4 x 102.
The value of any decimal number will be equal to the sum of its digits multiplied by their respective weights. For example, N = 616310 (Six Thousand One Hundred and Sixty-Three)  in a decimal format is equal to:
6000 + 100 + 60 + 3 = 6163
or it can be written reflecting the weight of each digit as:
( 6×1000 ) + ( 1×100 ) + ( 6×10 ) + ( 3×1 ) = 6163
or it can be written in polynomial form as:
( 6×103 ) + ( 1×102 ) + ( 6×101 ) + ( 3×100 ) = 6163
Where in this decimal numbering system example, the leftmost digit is the most significant digit or MSD, and the rightmost digit is the least significant digit or LSD. In other words, the digit 6 is the MSD since its leftmost position carries the most weight, and the number 3 is the LSD as its rightmost position carries the least weight.
Problem Solution
1. Take a binary number as input.
2. Multiply each digit of the binary number starting from the last with the powers of 2 respectively.
3. Add all the multiplied digits.
4. The total sum gives the decimal number.
Program to convert Binary into Decimal number
  1. /*
  2.  * C program to convert the given binary number into decimal
  3.  */
  4. #include <stdio.h>
  5.  
  6. void main()
  7. {
  8.     int  num, binary_val, decimal_val = 0, base = 1, rem;
  9.  
  10.     printf("Enter a binary number(1s and 0s) \n");
  11.     scanf("%d", &num); /* maximum five digits */
  12.     binary_val = num;
  13.     while (num > 0)
  14.     {
  15.         rem = num % 10;
  16.         decimal_val = decimal_val + rem * base;
  17.         num = num / 10 ;
  18.         base = base * 2;
  19.     }
  20.     printf("The Binary number is = %d \n", binary_val);
  21.     printf("Its decimal equivalent is = %d \n", decimal_val);
  22. }

Program Explanation
1. Take a binary number and store it in the variable num.
2. Initialize the variable decimal_val to zero and variable base to 1.
3. Obtain the remainder and quotient of the binary number. Store the remainder in the variable rem and override the variable num with quotient.
4. Multiply rem with the variable base. Increment the variable decimal_val with this new value.
5. Increment the variable base by 2.
6. Repeat the steps 3, 4 and 5 with the quotient obtained until quotient becomes zero.
7. Print the variable decimal_val as output.

Runtime Test Cases
 
Enter a binary number(1s and 0s)
10101001
The Binary number is = 10101001
Its decimal equivalent is = 169

Comments

Popular posts from this blog

Brick Breaker Game in JAVA

How to make C program to convert Fahrenheit into Celsius