0
0
CProgramBeginner · 2 min read

C Program to Convert Binary to Decimal Number

To convert binary to decimal in C, read the binary number as a string, then use a loop to multiply each digit by powers of 2 and sum them; for example, use decimal = decimal * 2 + (binary_digit - '0') inside a loop.
📋

Examples

Input101
Output5
Input1101
Output13
Input0
Output0
🧠

How to Think About It

To convert a binary number to decimal, think of each binary digit as a power of 2 starting from the right. Multiply each digit by 2 raised to its position index and add all results. Instead of calculating powers separately, you can multiply the current total by 2 and add the next digit as you move from left to right.
📐

Algorithm

1
Get the binary number as a string input.
2
Initialize a decimal result variable to 0.
3
For each character in the binary string:
4
Convert the character to a digit (0 or 1).
5
Multiply the current decimal result by 2 and add the digit.
6
After processing all digits, return the decimal result.
💻

Code

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

int main() {
    char binary[65];
    int decimal = 0;
    printf("Enter a binary number: ");
    scanf("%64s", binary);

    for (int i = 0; i < strlen(binary); i++) {
        decimal = decimal * 2 + (binary[i] - '0');
    }

    printf("Decimal: %d\n", decimal);
    return 0;
}
Output
Enter a binary number: 1101 Decimal: 13
🔍

Dry Run

Let's trace the binary number '1101' through the code

1

Initialize decimal

decimal = 0

2

Process first digit '1'

decimal = 0 * 2 + (1) = 1

3

Process second digit '1'

decimal = 1 * 2 + (1) = 3

4

Process third digit '0'

decimal = 3 * 2 + (0) = 6

5

Process fourth digit '1'

decimal = 6 * 2 + (1) = 13

IterationBinary DigitDecimal Value
111
213
306
4113
💡

Why This Works

Step 1: Reading input as string

We read the binary number as a string to process each digit individually.

Step 2: Converting digits to decimal

Each digit is converted from character to integer by subtracting '0', then added to the decimal total after multiplying the current total by 2.

Step 3: Accumulating result

Multiplying the current decimal value by 2 shifts it left by one binary place, then adding the digit adds the current bit value.

🔄

Alternative Approaches

Using pow() function
c
#include <stdio.h>
#include <string.h>
#include <math.h>

int main() {
    char binary[65];
    int decimal = 0, length;
    printf("Enter a binary number: ");
    scanf("%64s", binary);
    length = strlen(binary);

    for (int i = 0; i < length; i++) {
        if (binary[i] == '1') {
            decimal += (int)pow(2, length - i - 1);
        }
    }

    printf("Decimal: %d\n", decimal);
    return 0;
}
This method uses the math library's pow() to calculate powers of 2 explicitly but is less efficient due to repeated power calculations.
Using bit shifting
c
#include <stdio.h>
#include <string.h>

int main() {
    char binary[65];
    int decimal = 0;
    printf("Enter a binary number: ");
    scanf("%64s", binary);

    for (int i = 0; i < strlen(binary); i++) {
        decimal = (decimal << 1) | (binary[i] - '0');
    }

    printf("Decimal: %d\n", decimal);
    return 0;
}
This approach uses bitwise left shift and OR to build the decimal number, which is efficient and clear for binary operations.

Complexity: O(n) time, O(n) space

Time Complexity

The program processes each binary digit once in a loop, so the time complexity is O(n), where n is the length of the binary string.

Space Complexity

The program stores the binary number as a string, so space complexity is O(n). The decimal variable uses constant space.

Which Approach is Fastest?

The bit shifting method is generally fastest because it uses low-level operations without function calls, while the pow() method is slower due to repeated power calculations.

ApproachTimeSpaceBest For
Multiplying by 2 and adding digitO(n)O(n)Simple and clear for beginners
Using pow() functionO(n)O(n)Explicit power calculation but slower
Bit shiftingO(n)O(n)Efficient and suitable for binary operations
💡
Always validate that the input contains only '0' and '1' characters to avoid incorrect conversions.
⚠️
Beginners often forget to convert character digits to integers by subtracting '0', leading to wrong results.