C Program Converting Binary To Decimal Downloadbackstage

Downloadbackstage

C Program For Decimal To Binary Conversion using If – Else 21 22 23 24 25 26 27. Given with a binary number as an input, the task is to convert the given binary number into a decimal number. Decimal number in computers is represented with base 10 and binary number is represented with base 2 as it has only two binary digits 0 and 1 whereas decimal numbers can be any numeric digit starting from 0 – 9.

In this tutorial, we will learn about how to create a program in C that converts any given binary number (by user at run-time) to its equivalent decimal value. At last we have also created the same purpose program using user-defined function named BinToDec().

But before going through the program, if you are not aware of

  • Binary Number
  • Decimal Number
  • Binary to Decimal Conversion

then refer to Binary to Decimal conversion step by step process. Now let's move on to the program.

Binary to Decimal in C

To convert binary number to decimal number in C programming, you have to ask from user to enter the binary number and then convert it into decimal number as shown in the following program:

As the above program was written under Code::Blocks IDE, therefore after successful build and run, you will get the following output. This is the first snapshot of the sample run:

Now supply any binary number say 101110 and press ENTER key to see its equivalent value in decimal number system as shown in the second snapshot of the sample run given below:

Program Explained

C Program Converting Binary To Decimal Downloadbackstage
  • Receive any binary number from user at run-time say 101110
  • Create a while loop that runs until the value of binnum (entered binary number from user say 101110) becomes 0
  • In other words, the loop continues running until the value of binnum does not becomes equal to 0. And when the value of binnum gets equal to 0, then the program flow goes out from the loop or exits from the while loop
  • At first run of the while loop, the condition binnum!=0 or 101110!=0 evaluates to be true, therefore program flow goes inside the loop, and binnum%10 or 101110%10 or 0 gets initialized to rem
  • And decnum + rem*pow(2, i) (we have initialized decnum and i both with 0 at start of the program. And the function pow() of math.h library takes two arguments, first argument corresponds to base and second argument corresponds to exponent. That is, in 23, 2 is base and 3 is exponent) or 0 + 0*pow(2, 0) or 0 + 0*20 or 0 + 0*1 or 0+0 or 0 gets initialized to decnum
  • The value of i gets incremented and becomes 1
  • And then binnum/10 or 101110/10 or 10111 gets initialized to binnum
  • Now program flow goes back to the condition of the while loop. That is binnum!=0 or 10111!=0 evaluates to be true, therefore program flow again goes inside the loop
  • Inside the loop, binnum%10 or 10111%10 or 1 gets initialized to rem
  • And then decnum + rem*pow(2, i) or 0 + 1*pow(2, 1) or 0 + 1*21 or 2 gets initialized to decnum
  • The value of i gets incremented and becomes 2
  • And binnum/10 or 10111/10 or 1011 gets initialized to binnum
  • Now the program flow goes back to the condition of the while loop again. There binnum!=0 or 1011!=0 evaluates to be true, therefore program flow again goes inside the loop
  • Therefore again binnum%10 or 1011%10 or 1 gets initialized to rem
  • And then decnum + rem*pow(2, i) or 2 + 1*pow(2, 2) or 2 + 1*22 or 2 + 1*4 or 6 gets initialized to decnum
  • The value of i gets incremented and becomes 3. And then binnum/10 or 1011/10 or 101 gets intialized to binnum
  • In this way, program flow again goes back to the condition of the while loop and if the condition evaluates to be true, then follow the similar steps as told above to calculate the new value of rem, decnum, i, and binnum.
  • That is, at fourth run of the while loop, rem, decnum, i, binnum holds its value as 1, 14, 4, 10 respectively
  • Then at fifth run, rem, decnum, i, binnum holds 0, 14, 5, 1 respectively
  • And at sixth run, rem, decnum, i, binnum holds 1, 46, 6, 0 respectively
  • As the value of binnum becomes equal to 0, therefore program flow does not goes inside the loop. And we have 46 as the value of decnum which is the equivalent decimal value of given binary number say 101110
  • Now print the value of decnum at last of the program
How to convert decimal to binary

without pow() Function

Now let's create the same program, but this time without using any pow() function of math.h library. The question is, Write a program in C that converts any given binary number to decimal number without using any pow() function. The answer to this question is:

Here is the final snapshot of the sample run of above program:

Here we have replaced pow(2, i) (0 as initial value of i) with i (1 as initial value of i) and i++ with i=i*2. Therefore, here in this case, we will get:

  • At first run, i equals 1
  • At second run, i equals i*2 or 1*2 or 2
  • At third run, i equals i*2 or 2*2 or 4
  • At fourth run, i equals i*2 or 4*2 or 8
  • At fifth run, i equals i*2 or 8*2 or 16
  • At seventh run, i equals i*2 or 16*2 or 32

Here we are getting the same values in case of pow(2, i), that are:

  • At first run, pow(2, 0) or 20 or 1
  • At first run, pow(2, 1) or 21 or 2
  • At first run, pow(2, 2) or 22 or 4
  • At first run, pow(2, 3) or 23 or 8
  • and so on

using User-defined Function

Now let's create another program in C that using user-defined function named BinToDec() to convert any given binary number into its equivalent decimal value as shown in the program given below:

Here is the final snapshot of the sample run:

Here we have used a function named BinToDec() that takes one argument as binary number. Inside the function we have converted the given binary number to decimal number. The decimal number holds inside a variable dec. And we have returned the value of dec to the function. Therefore inside the main() function, we have initialized the returning value of function BinToDec() to a variable decnum that holds the value of dec of BinToDec() function. Finally print the value of decnum as output that will be the equivalent decimal value of given binary number.

Same Program in Other Languages


Decimal to binary in C: We can convert any decimal number (base-10 (0 to 9)) into binary number(base-2 (0 or 1)) by c program.

Decimal Number

C How To Convert Binary To Decimal

Decimal number is a base 10 number because it ranges from 0 to 9, there are total 10 digits between 0 to 9. Any combination of digits is decimal number such as 23, 445, 132, 0, 2 etc.

Binary Number

Binary number is a base 2 number because it is either 0 or 1. Any combination of 0 and 1 is binary number such as 1001, 101, 11111, 101010 etc.

Let's see the some binary numbers for the decimal number.

DecimalBinary
11
210
311
4100
5101
6110
7111
81000
91001
101010

Decimal to Binary Conversion Algorithm

  • Step 1: Divide the number by 2 through % (modulus operator) and store the remainder in array
  • Step 2: Divide the number by 2 through / (division operator)
  • Step 3: Repeat the step 2 until number is greater than 0

Let's see the c example to convert decimal to binary.

C Program Converting Binary To Decimal Downloadbackstage

Decimal To Binary C++ Code

Output:

Next TopicC Program to convert Number in Characters