Program to Convert Binary to Decimal in C

program-to-convert-binary-number-to-decimal-number

Program Description:

Write a Program to convert a given number from Binary to Decimal in C programming language. The program should accept a Binary Number (zero(0)’s and 1’s) and convert it to decimal number. The program should also display the error message if the user enters Invalid numbers.

Before going to write the program, we should know what are binary numbers.

What is a Binary Number?

The Binary numeral system is a Base-2 number system and uses only two digits Zero (0)’s and One (1)’s.

We normally use the decimal number system, Which is also called as Base-10 number system.

In the Binary system, each place is a power of two.

How to Convert Binary Number to Decimal Number?

The binary representation of a decimal number 21 is equal to 10101 (binary number)

Here is the step-by-step process of converting a Binary number( 10101) to Decimal Number( 21).

101012 = [ ( 1 ) × 24 ] + [ ( 0 ) × 23 ] + [ ( 1 ) × 22 ] + [ ( 0 ) × 21 ] + [ ( 1 ) × 20 ]

101012 = [ 1 × 16 ] + [ 0 × 8 ] + [ 1 × 4 ] + [ 0 × 2 ] + [ 1 × 1 ]

101012 = 2110

Here is a few binary numbers and their equivalent decimal values

Binary NumberDecimal Number
00
11
102
113
1004
1015
1106
1117
10008
10019
101010
101111
110012
110113
111014
111115

Program Expected Input and Output:

Input:

Enter binary number (0's and 1's) : 1010

Output:

binary number : 1010 is equal to decimal number : 10

Pre-Requisites:

We are going to use Arithmetic Operators like Modulus, Division operators, and C Loops. Please go through the following articles to learn more about them.

Program to Convert Binary Number to Decimal Number Algorithm:

  1. Take the input binary number from the user and store it in num variable
  2. Check if the num is positive or not using num <= 0 condition. If num is a negative number, display an error message – Error: Please enter valid binary number
  3. The logic is to get each digit of the binary number num and use the power function to calculate the power of 2 (as it is a binary number) with respect to the digit at the given position.
  4. We have a variable decimal, which contains the converted number so far, and variable ‘i’, which starts from and is incremented by 1 at each iteration. So we can use i to calculate the power.
  5. Start the Loop and iterate until the num > 0. At Each Iteration
    • We get the last digit of the number ( num) at each iteration. For the first iteration, We get the last digit using the modulus operator – rem = num % 10;
    • Then we calculate the rem * pow(2, i);. So At the first Iteration, which is equal to pow(2, 0) and at the second iteration, It will be pow(2, 1), and so on.
    • Then add the above result to our decimal variable – decimal = decimal + rem * pow(2, i);
    • As we processed the last digit, We need to remove it from the  num. To do that, We can use the Division Operation like this – num = num / 10;
    • Finally, Increment the variable  i. which is used in the calculation of  pow function.
  6. The above loop continues until the  num value is greater than the  Zero
  7. Once the loop is terminated, The variable decimal contains the converted number. Display the result on the console.

Program to Convert Binary to Decimal in C using while loop:

We are going to use the pow function from the math.h header file. So don’t forget to add the math.h header file to your program. Here is the prototype of the  pow function is

double pow(double x, double y)

If you don’t want to use the inbuilt pow function, you can check the following article, where we wrote a power function manually

Decimal to Binary Conversion Program Output:

We used the  pow function to calculate the power value. And  pow function is not part of the default C library, So we need to use the  -lm option to include the math library functions.

So to Compile the program use the following command ( In Linux based systems)

$ gcc binary-to-decimal.c -lm

The above command generates the executable file (  a.out), Run the executable.

As you can, We are getting the desired results.

Let’s test the negative number case

The program is displaying the Error message on a Negative number

Binary to decimal in C – Only Allow Zero’s and One’s:

What if the user provides digits other than the zeroes and ones? Let’s add another check to our program and accept only the numbers with digits Zero’s and One’s and display an error message if any other digit presents in the given number.

Example:

Input:

Enter binary number (0's and 1's) : 12414001

Output:

We need to make a change to the above program, To check if the digit rem (last digit) is equal to Zero(0) or One(1).

If the rem is not equal to both numbers, Then the provided number num is not a valid binary number. So display the error message and stop the program.

if(rem != 0 && rem != 1)

So Let’s make the above change to our Binary to Decimal number conversion program

Program Output:

Now, The program should display the error message if num contains any digit other than the binary digits i.e Zero(0)’s and One(1)’s.

📢 Add the -lm option during the program compilation

Let’s start with the Negative test case by providing a number with other digits then Zero(0)’s and One(1)’s.

As you can see from the above example When the user entered 12414001 as the number, Program detected it as not a binary number and displayed the error message Error: Got non-binary Digit (4) and terminated the program.

A similar thing happened for the second and third examples, Where we provided 12001, 9109001. The Program detected and stopped the program.

Let’s also test the positive test cases

As we can see from the above output, We are getting the expected outputs and the program is able to convert the binary number to decimal number correctly.

program-to-convert-binary-number-to-decimal-number-in-c-output

Program to Convert Binary to Decimal in C Language using for loop:

Let’s write the binary number to decimal number conversion program using the for loop instead of the while loop. The program logic is not going to change. But we are going to use the for loop in this case.

Program Output:

Test the basic test cases by providing valid binary numbers.

if the user provides a negative number or non-binary digits

The program is displaying errors on Negative numbers or Non-binary digits.

Prompt the user until the valid Input:

Use the Goto Statement to prompt the user again for the input in case of Invalid Input.

Modify the above program and the goto statement to jump to the start of the program in case of Invalid Input.

Program Output:

As you can see from the above output, The program is displaying the error and allowing the user to provide the input number again.

Learn More about Binary Numbers:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

6 Responses

  1. […] C Program to Convert Binary Number to Decimal Number […]

  2. […] 📢 Related Program: Convert Binary to Decimal using loops […]

  3. […] C Program to Convert Binary Number to Decimal Number […]

  4. […] C Program to Convert Binary Number to Decimal Number […]

  5. […] C Program to Convert Binary Number to Decimal Number […]

  6. […] C Program to Convert Binary Number to Decimal Number […]

Leave a Reply