C Program to Convert Binary Number to Decimal

C




// C program to convert binary to decimal
#include <stdio.h>
  
// Function to convert binary to decimal
int binaryToDecimal(int n)
{
    int num = n;
    int dec_value = 0;
  
    // Initializing base value to 1, i.e 2^0
    int base = 1;
  
    int temp = num;
    // Extracting the last digit of the binary number
    while (temp) {
        int last_digit = temp % 10;
        // Removing the last digit from the binary number
        temp = temp / 10;
  
        // Multiplying the last digit with the base value
        // and adding it to the decimal value
        dec_value += last_digit * base;
  
        // Updating the base value by multiplying it by 2
        base = base * 2;
    }
  
    // Returning the decimal value
    return dec_value;
}
  
// Driver program
int main()
{
    int num = 10101001;
    printf("%d", binaryToDecimal(num));
}


Output

169

Complexity Analysis

  • Time complexity: O( log n)
  • Auxiliary Space: O(1)

Note: In the above program, we represented a binary number as integer value with base 10 as binary numbers are not directly supported by C language.

Refer to the complete article Program for Binary To Decimal Conversion for more details!

Related Articles



Convert Binary to Decimal in C

In this article, we will learn how to write a C program to convert the given binary number into an equivalent decimal number. Binary numbers are expressed in base 2 ( 0, 1 ) and decimal numbers are expressed in base 10 ( 0-9 ).

Similar Reads

Algorithm to Convert Binary Numbers to Decimal

The idea is to extract the last digit of the binary number by performing the modulo operation ( % ) and store it in a variable last_digit and remove the last digit from the binary number by dividing by 10. Update the decimal value by multiplying last_digit with the current base value and adding it to dec_value. Update the base value by multiplying it by 2 to represent the next power of 2 for the next digit. Repeat these steps until are digits of the binary number are processed. Return the variable dec_value that stores the decimal value....

C Program to Convert Binary Number to Decimal

C // C program to convert binary to decimal #include    // Function to convert binary to decimal int binaryToDecimal(int n) {     int num = n;     int dec_value = 0;        // Initializing base value to 1, i.e 2^0     int base = 1;        int temp = num;     // Extracting the last digit of the binary number     while (temp) {         int last_digit = temp % 10;         // Removing the last digit from the binary number         temp = temp / 10;            // Multiplying the last digit with the base value         // and adding it to the decimal value         dec_value += last_digit * base;            // Updating the base value by multiplying it by 2         base = base * 2;     }        // Returning the decimal value     return dec_value; }    // Driver program int main() {     int num = 10101001;     printf("%d", binaryToDecimal(num)); }...