Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# C Program to Convert Binary Number to Decimal Number

Last Updated on November 1, 2023 by Ankit Kochar

Within the realm of programming, the process of transforming binary numbers into decimal numbers serves as a vital operation, enabling data to be presented in a human-readable format. Binary numbers, constituting the base-2 numerical system, are typically denoted using only two symbols: 0 (zero) and 1 (one). On the other hand, decimal numbers adhere to the base-10 numerical system, utilizing digits spanning from 0 to 9 to represent numerical values.

## What is Conversion from Binary To Decimal In C?

Conversion from binary to decimal in C means the user needs to convert a number that is in binary form to its value which will be in decimal form.
Take a break, lets discuss what are binary and decimal numbers then we will see the approaches to convert binary to decimal in c.

## What is a Binary Number?

A binary number is characterized as a numerical representation within the binary system, also known as the base-2 numeral system. It conveys numeric values using a pair of distinct symbols: 1 (representing one) and 0 (representing zero).

Examples of Binary Numbers: 0101010, 111, 0010, etc.

## What is a Decimal Number?

Decimal is a term that describes the base-10 number system, the most commonly used number system. The decimal number system consists of ten single-digit numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. The number after 9 is 10. The number after 19 is 20 and so forth.

Examples of Decimal Numbers: 1415, 1616, 472, etc.

## Examples demonstrating Binary to Decimal in C

The following examples demonstrates the conversion of binary to decimal.

Example 1:

Input: 111
Output: 7
Explanation: (122) + (121)+(1*20) = 4 + 2 + 1 = 7

Example 2:

Input: 1010
Output: 10

Example 3:

Input: 110
Output: 6

### Approach 1: Conversion of Binary To Decimal In C using For Loop

In this approach, we are using the for a loop to convert binary to decimal in C language.

For Example:
If the binary number is 111.
dec_value = 1(2^2) + 1(2^1) + 1*(2^0) = 7

Algorithm of Binary To Decimal In C using for loop

1. Firstly, initialize the decimal_num variable with the value 0, add the binary number in the bin_num variable, and initialize rem.
2. Start a for loop with condition i = 0, bin_num != 0, ++i.
3. In the loop block,
• Update the rem value to bin_num % 10.
• Change the value of bin_num with bin_num / 10.
• Atlast change the value of decimal_num with decimal_num + (rem) * (pow(2, i)).
4. At the end of the for loop, print the decimal_num variable, which has the value of the decimal number of the given binary number.

Code Implementation of For Loop approach for converting of Binary To Decimal In C

```#include <stdio.h>
#include <math.h>
int main()
{
int i, bin_num, decimal_num = 0, rem;
bin_num = 111;
for (i = 0; bin_num != 0; ++i)
{
rem = bin_num % 10;
bin_num = bin_num / 10;
decimal_num = decimal_num + (rem) * ( pow (2, i));

}
printf ("\n Decimal Number of given binary number: %d", decimal_num);
return 0;
}
```

Time Complexity of C program to convert binary to decimal: The time complexity of this implementation of the conversion of binary to decimal in C language is O(logN).

Space Complexity of C program to convert binary to decimal: The space complexity of this implementation of the conversion of binary to decimal in C language is O(1).

### Approach 2: Conversion of Binary To Decimal In C using Recursion

In this approach, we are using the recursive function to convert binary to decimal in C. BinarytoDecimal(strBinary, length) takes the input string and its size, and for each character convert it to decimal and multiply it with 2i. Add previous results to it.

Algorithm for the conversion of Binary To Decimal In C using recursion:

T1. ake the input string strBinary[] containing a binary number.

1. Calculate its length using strlen(strBinary).
2. Function BinarytoDecimal(strBinary, length) takes input and returns the number calculated using a recursive approach.
3. If we are at the last character which is LSB, then return its decimal as it will be the same. (multiplied by 1 i.e 20 )
4. Otherwise set temp=binary[i] – ‘0’. It’s a decimal value.
5. Now multiply temp with 2len-i-1 using temp << len – i – 1.
6. Add the result of other digits to temp using temp = temp + BinarytoDecimal(binary, len, i + 1).
7. At the end of the recursion return temp.
8. Print the calculated decimal in the main.

Code Implementation of Recursive approach for converting Binary to Decimal in C:

```#include
#include
int BinarytoDecimal(char binary[],int len, int i){
if (i == len-1)
return (binary[i] - '0');

int temp=binary[i]-'0';
temp=temp<
```

Time Complexity of C program to convert binary to decimal: The time complexity of this implementation of converting a binary number into a decimal number is O(logN).

Space Complexity of C program to convert binary to decimal: The space complexity of this implementation of converting a binary number into a decimal number is O(1).

### Approach 3: Conversion of Binary to Decimal in C using Function

Let’s Discuss the algorithm for converting binary to decimal in C language.

1. A binary number is stored in the bin_num variable, and one other variable dec_num is initialized for storing the decimal number.
2. BinaryTodecimal function will be called and bin_num is passed in it.
3. In the function, decimal_num with 0, temp with 0, and rem are initialized.
4. Then a while loop is started with the condition bin_num != 0.
5. Inside the loop, the following steps will be done:
• Change the value of rem to bin_num % 10.
• Change the value of bin_num to bin_num / 10.
• Change the value of decimal_num to decimal_num + rem * pow( 2, temp).
• Change the value of temp to temp++.
6. At last return the decimal_num variable.

Code Implementation for converting Binary to Decimal in C using Function:

```#include <stdio.h>

int binaryTodecimal(int bin_num);
int main()
{
int bin_num, dec_num;
bin_num = 111;

dec_num = binaryTodecimal (bin_num);
printf (" Conversion of the binary number to decimal number is %d", dec_num);
return 0;
}

int binaryTodecimal( int bin_num)
{
int decimal_num = 0, temp = 0, rem;
while (bin_num != 0)
{
rem = bin_num % 10;
bin_num = bin_num / 10;
decimal_num = decimal_num + rem * pow( 2, temp);
temp++;
}
return decimal_num;
}
```

Time Complexity of C program to convert Binary to Decimal: The time complexity of this implementation of converting a binary number into a decimal number is O(logN).

Space Complexity of C program to convert Binary to Decimal: The space complexity of this implementation of converting a binary number into a decimal number is O(1).

Conclusion
In the realm of C programming, the conversion of binary numbers to decimal numbers is a fundamental task that enables the transformation of binary representations into their equivalent decimal values. This process is essential for various applications, particularly in scenarios where binary data needs to be interpreted or displayed in a human-readable format. We have explored different methods and approaches to achieve this conversion, providing valuable insights into the logic and techniques involved.

## Frequently Asked Questions (FAQs) related to C Program to Convert Binary Number to Decimal Number

Here are some Frequently Asked Questions on “How to Convert Binary to Decimal Program in C”.

1. Why is it important to convert binary numbers to decimal in C programming?
Converting binary numbers to decimal is crucial for interpreting binary data and presenting it in a format that is easier for humans to understand. This is particularly valuable when working with low-level data or in scenarios where binary data needs to be converted for display or further processing.

2. What is the formula to convert a binary number to a decimal number?
The formula to convert a binary number to decimal involves multiplying each binary digit by the corresponding power of 2 and summing up the results. For example, to convert the binary number "1010" to decimal: (1 2^3) + (0 2^2) + (1 2^1) + (0 2^0) = 8 + 0 + 2 + 0 = 10.

3. Are there built-in functions in C for converting binary to decimal?
C does not provide built-in functions specifically for this conversion, so custom logic is typically required.

4. What is the largest binary number that can be converted to decimal using this method?
The size of the binary number that can be converted to decimal using this method depends on the data type used to store the binary value. For example, with a 32-bit integer data type, you can convert binary numbers up to 2^32 – 1 to decimal.