In this article, We will convert Binary to decimal C program, To make it in a human-readable format. Binary numbers are the base – 2 number system numerals, which are generally represented by just two digits/symbols: 0 (Zero) and 1 (One), while decimal numbers are the base – 10 number system numbers, which are represented by numbers formed by digits in the range of 0 – 9.

## What a Binary Number is.

A binary number is defined as the number that is expressed in the binary system or base 2 numeral system. It describes numeric values by two separate symbols; 1 (one) and 0 (zero).

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

### Example:

**Input1:** 111

**Output1:** 7

**Explanation:** (1*22) + (1*21)+(1*20) = 4 + 2 + 1 = 7

**Input2:** 1010

**Output2:** 10

**Input3:** 110

**Output3:** 6

## Approach 1: Conversion of Binary to Decimal in C using For Loop

In this approach, we are using the for a loop.

**For Example:**

If the binary number is 111.

dec_value = 1*(2^2) + 1*(2^1) + 1*(2^0) = 7

### Algorithm for conversion of binary to decimal in c using for loop

- Firstly, initialize the decimal_num variable with the value 0, add the binary number in the bin_num variable, and initialize rem.
- Start a for loop with condition i = 0, bin_num != 0, ++i.
- 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)).

- 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 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:** The time complexity of this implementation of converting a binary number into a decimal number is O(logN).

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

## Approach 2: Conversion of Binary to Decimal in C using Recursion

In this approach, we are using the recursive function. 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:

- Take the input string strBinary[] containing a binary number.
- Calculate its length using strlen(strBinary).
- Function BinarytoDecimal(strBinary, length) takes input and returns the number calculated using a recursive approach.
- 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 )
- Otherwise set temp=binary[i] – ‘0’. It’s a decimal value.
- Now multiply temp with 2len-i-1 using temp << len – i – 1.
- Add the result of other digits to temp using temp = temp + BinarytoDecimal(binary, len, i + 1).
- At the end of the recursion return temp.
- 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:** The time complexity of this implementation of converting a binary number into a decimal number is O(logN).

**Space Complexity:** 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 numbers into decimal numbers.

- A binary number is stored in the bin_num variable, and one other variable dec_num is initialized for storing the decimal number.
- BinaryTodecimal function will be called and bin_num is passed in it.
- In the function, decimal_num with 0, temp with 0, and rem are initialized.
- Then a while loop is started with the condition bin_num != 0.
- 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++.

- 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:** The time complexity of this implementation of converting a binary number into a decimal number is O(logN).

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

**Conclusion**

This blog taught us the most efficient and different ways to convert binary to decimal C program. We hope this article clears all your doubts and enhances your knowledge. Also, if you want to solve more similar problems you can check our MYCODE platform. These questions are prepared by our expert mentors.

**Other C Programs**

C program to calculate percentage of 5 subjects

C program to add two numbers

C program to convert celsius to fahrenheit

C program to convert infix to postfix

C program to find area of circle

C program to find roots of quadratic equation

C program to reverse a number

C program for merge sort for linked lists

C program for performing bubble sort on linked list

C program to reverse a linked list