# Program to Convert Binary to Decimal in C

## 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

## 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 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.

Venkatesh

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

### 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 [â€¦]