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!

Top 3 Ways To Convert Binary To Decimal In C

Last Updated on April 26, 2023 by Prepbytes

In computer programming, binary and decimal are two common number systems. The binary is a base-2 system, whereas the decimal is a base-10 system. Binary numbers are represented using only two digits, 0 and 1, whereas decimal numbers are represented using ten digits, 0 through 9. In this article, we will discuss how to convert binary to decimal in C programming language.

What is a Binary Number?

A binary number is a numerical system that uses only two digits: 0 and 1. Each digit in a binary number represents a power of 2, starting from the rightmost digit representing 2^0, then the next digit representing 2^1, and so on.

For example, the binary number "101" represents the decimal value of 5 because (1 2^2) + (0 2^1) + (1 2^0) = 4 + 0 + 1 = 5. In the same way, "11010" represents the decimal value of 26 because (1 2^4) + (1 2^3) + (0 2^2) + (1 2^1) + (0 2^0) = 16 + 8 + 0 + 2 + 0 = 26.

Binary numbers are widely used in computer science and digital electronics, as they can represent the two states of an electronic circuit – on and off. They are also used in various applications, such as data storage, communication, cryptography, and arithmetic operations.

What is a Decimal Number?

A decimal number is a number system that uses ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Each digit in a decimal number represents a power of 10, starting from the rightmost digit representing 10^0, the next digit representing 10^1, and so on.

For example, the decimal number "123" represents the value of 1 10^2 + 2 10^1 + 3 10^0 = 100 + 20 + 3 = 123. Similarly, "4567" represents the value of 4 10^3 + 5 10^2 + 6 10^1 + 7 * 10^0 = 4000 + 500 + 60 + 7 = 4567.

Decimal numbers are widely used in everyday life, such as money, time, weights, and measures. In computer programming, decimal numbers are commonly used to represent floating-point numbers and monetary values.

Ways to Convert Binary To Decimal in C

Now, we will discuss some ways to convert binary to decimal in C programming language.

1. Convert Binary to Decimal in C Using Recursion

Recursion is a programming technique where a function calls itself repeatedly until a base case is reached. In this approach, we are using the recursive function BinarytoDecimal(strBinary, length) which takes the input string and its length and for each character converts it to decimal and multiplies it with 2, and add previous results to it.

  1. Take the input string strBinary[] containing a binary number.
  2. Calculate its length using strlen(strBinary).
  3. Function BinarytoDecimal(strBinary, length) takes input and returns the number calculated using a recursive approach.
  4. 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 )
  5. Otherwise set temp=binary[i] – ‘0’. It’s a decimal value.
  6. Now multiply temp with 2len-i-1 using temp << len – i – 1.
  7. Add the result of other digits to temp using temp = temp + BinarytoDecimal(binary, len, i + 1).
  8. At the end of recursion return temp.
  9. Print the calculated decimal in the main.

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

The below diagram explains how to convert ( 1010 ) to an equivalent decimal value:

Here’s the code to convert binary to decimal in C using recursion

#include <stdio.h>
#include <string.h>
int BinarytoDecimal(char binary[],int len, int i){
   if (i == len-1)
   return (binary[i] - '0');

   int temp=binary[i]-'0';
   temp=temp<<len-i-1;
   temp=temp+BinarytoDecimal(binary,len,i+1);
   return (temp);
}
int main(){
   char strBinary[] = "111";
   int length=strlen(strBinary);
   printf("Decimal Number of given binary number: %d",BinarytoDecimal(strBinary,length,0));
   return 0;
}

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

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

2. Convert Binary to Decimal in C Using For Loop

Another way to convert binary to decimal in C is by using a for loop. Let’s discuss this approach in detail in order to convert binary to decimal in C.

  1. Firstly, initialize the decimal_num variable with the value 0, add the binary number in the bin_num variable, and initialize rem.
  2. Now, 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 is having the value of the decimal number of the given binary number.

Here’s the code to convert binary to decimal in C using a for loop

#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 program which convert binary to decimal in C is O(logN).

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

3. Convert Binary to Decimal in C using Function

We can also create a separate function to convert binary to decimal in C. Let’s discuss this approach in detail in order to convert binary to decimal in C.

  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.

Here’s the code to convert 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 program which convert binary to decimal in C is O(logN).

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

4. Convert Binary to Decimal in C Using Array

In this approach, we convert the binary number to an integer array, where each element of the array represents a binary digit. We then traverse the array from right to left and calculate the decimal value by adding the product of each binary digit with the corresponding power of 2.

Here is the code to convert binary to decimal in C using array.

#include <stdio.h>

int binaryToDecimal(int binary[], int size) {
    int decimal = 0;
    int power = 1;
    for (int i = size - 1; i >= 0; i--) {
        decimal += binary[i] * power;
        power *= 2;
    }
    return decimal;
}

int main() {
    int binary[] = {1, 0, 1, 1, 0, 1};
    int size = sizeof(binary) / sizeof(binary[0]);
    int decimal = binaryToDecimal(binary, size);
    printf("Decimal value of binary number: %d", decimal);
    return 0;
}

Output:

Decimal value of binary number: 45

Time Complexity:
The for loop in the binaryToDecimal function iterates through each element of the array, performing a constant number of operations (multiplication and addition) for each iteration. Therefore, the time complexity of this function is O(n), where n is the size of the array.

The main function has constant time complexity, as it only calls the binaryToDecimal function and prints the result. Therefore, the overall time complexity of the program is O(n).

Space Complexity:
The program uses a constant amount of space for storing the binary array, the decimal variable, and the power variable. Therefore, the space complexity of the program is O(1), which means that the amount of memory used does not depend on the size of the input.

Conclusion
In this article, we discussed different approaches to convert binary to decimal in C using array, recursion, for loop, and function. All of these methods are valid and efficient, depending on the specific use case. It is important to understand the different approaches and choose the one that best suits the needs of the program.

FAQs

Here are some frequently asked questions on the conversion of binary to decimal in C:

Q1: Can fractional binary numbers be converted to decimal using these methods?
Ans: No, the methods for converting binary to decimal in C discussed here only work for integer binary numbers. To convert fractional binary numbers to decimals, you need to use a different method, such as multiplying each bit by the appropriate power of 2^-1, starting from the first bit after the decimal point.

Q2: Is there a built-in function to convert binary to decimal in C?
Ans: No, there is no built-in function to convert binary to decimal in C. You need to use a custom function or algorithm to perform this conversion.

Q3: How can I check if my binary to decimal conversion is correct?
Ans: You can verify the correctness of your binary to decimal conversion by converting the decimal equivalent back to binary using the reverse process and comparing it to the original binary number. If the two binary numbers are identical, your conversion is correct.

Q4: What is the time complexity of the methods which convert binary to decimal in C?
Ans: The time complexity of the methods which convert binary to decimal in C is O(n), where n is the number of bits in the binary number.

Leave a Reply

Your email address will not be published. Required fields are marked *