Open In App

Convert Binary to Decimal in C

Last Updated : 02 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

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.
  • 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 that stores the decimal value.

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

binary to decimal in c

C Program to Convert Binary Number to Decimal

C
#include <stdio.h>

int binaryToDecimal(int n) {
    int dec = 0;

    // Initializing base value to 1, i.e 2^0
    int base = 1;
    
    // Extracting each digits of binary number
    // and adding corresponding exponent of 2
    while (n) {
        int last_digit = n % 10;
        n = n / 10;

        // Multiplying the last digit with the base value
        // and adding it to the decimal value
        dec += last_digit * base;

        // Updating the base value by multiplying it by 2
        base = base * 2;
    }

    return dec;
}

int main() {
    int num = 10101001;
    printf("%d", binaryToDecimal(num));

    return 0;
}

Output
169

Time complexity: O(d), where d is the number of digits in binary number.
Auxiliary Space: O(1)

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. One more common representation of binary number is in the form of strings. If the binary number is in the form of string, then the above program can be modified as shown:

C
#include <stdio.h>
#include <string.h>

int binaryToDecimal(const char* binary) {
    int dec = 0;
    
    // Get length of binary string
    int length = strlen(binary);
    
    // Initializing base value to 1, i.e 2^0
    int base = 1;
    
    // Process from right to left (least significant to
    // most significant bit)
    for (int i = length - 1; i >= 0; i--) {
        
        // If current bit is '1'
        if (binary[i] == '1') {
            dec += base;
        }
        
        // Update base for next position (multiply by 2)
        base = base * 2;
    }
    
    return dec;
}

int main() {
    const char* binary = "10101001";
    printf("%d\n", binaryToDecimal(binary));
    
    return 0;
}

Output
169

Time complexity: O(d), where d is the number of digits in binary number.
Auxiliary Space: O(1)

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



Next Article

Similar Reads