Binary to Decimal in C


In this section, we will create different programs to convert a binary number to a decimal number in C.

Let's first understand what is a binary number and what is a decimal number.

Binary Numbers

A binary number is a number expressed in the base-2 numeral system or binary numeral system, which uses only two symbols: typically 0 (zero) and 1 (one).

For example, 1011 is a binary number. It is equal to 1*23 + 0*22 + 1*21 + 1*20 = 8 + 0 + 2 + 1 = 11.

Binary numbers are used in computers and other digital devices. The binary number system is the most basic computer language.

Decimal Numbers

A decimal number is a number that is expressed in the base-10 numeral system or decimal numeral system, which uses ten symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

For example, 123 is a decimal number. Decimal numbers are used in our day-to-day life.


Binary to Decimal Conversion

To convert a binary number to a decimal number, take the binary number and multiply each digit with 2n where n is the position of the digit from the right side.

(10)2 = 1*21 + 0*20 = 2
(11)2 = 1*21 + 1*20 = 3
(101)2 = 1*22 + 0*21 + 1*20 = 5
(1011)2 = 1*23 + 0*22 + 1*21 + 1*20 = 11
binary to decimal conversion
Binary to Decimal Conversion

C Program to Convert Binary to Decimal

Let's create a C program to convert a binary number to a decimal number.

Method 1: Getting Binary Number as Integer

In this method, we will create a function that uses a while loop to iterate through each digit of the binary number and convert it to a decimal number.

Algorithm

  1. Create a function and pass the binary number as an argument.
  2. Declare a variable decimal and initialize it to 0 and a variable base and initialize it to 1.
  3. Create a while loop that runs until the binary number is greater than 0.
  4. Inside the while loop, get the last digit of the binary number using the modulus operator and multiply it with the base and add it to the decimal variable.
  5. Divide the binary number by 10 and multiply the base by 2.
  6. Return the decimal number.

Let's see the program.

#include <stdio.h>

// function to convert binary to decimal
int bin2dec(int bin){
    int dec = 0;
    int base = 1;
    while(bin != 0){
        dec += (bin % 10) * base;
        bin /= 10;
        base *= 2;
    }
    return dec;
}

int main() {
  printf("Enter a binary number: ");
  int bin;
  scanf("%d", &bin);

  // convert binary to decimal
  int dec = bin2dec(bin);
  printf("Decimal value of %d is %d", bin, dec);
  return 0;
}
binary to decimal in c example 1
Binary to Decimal in C Example 1

Output:

Enter a binary number: 10101
Decimal value of 10101 is 21

Method 2: Getting Binary Number as String

Binary numbers are usually represented as strings because they can have a large number of digits.

In this method, we will take the binary number as a string and convert it to a decimal number.

Algorithm

  1. Create a function and pass the binary string as an argument.
  2. Declare a variable decimal and initialize it to 0.
  3. Get the length of the binary number using the strlen() function.
  4. Create a loop that takes the character of binary string from right to left and checks if it is 1 or 0.
  5. If the character is 1, add 2length - index - 1 to the decimal variable.
  6. Return the decimal number.
  • for loop
#include <stdio.h>
#include <math.h>

// function to convert binary to decimal
int bin2dec(char bin[]){
    int dec = 0;
    int i = 0;
    int len = strlen(bin);

    while (bin[i] != '\0'){
        if (bin[i] == '1'){
            dec += pow(2, len - i - 1);
        }
        i++;
    }

    return dec;
}

int main() {
    printf("Enter a binary number: ");
    char bin[100];
    scanf("%s", bin);

    // convert binary to decimal
    int dec = bin2dec(bin);
    printf("Decimal value of %s is %d", bin, dec);
    return 0;
}
#include <stdio.h>
#include <math.h>

// function to convert binary to decimal
int bin2dec(char bin[]){
    int dec = 0;
    int i = 0;
    int len = strlen(bin);

    for(i = 0; i < len; i++){
        if (bin[i] == '1'){
            dec += pow(2, len - i - 1);
        }
    }

    return dec;
}

int main() {
    printf("Enter a binary number: ");
    char bin[100];
    scanf("%s", bin);

    // convert binary to decimal
    int dec = bin2dec(bin);
    printf("Decimal value of %s is %d", bin, dec);
    return 0;
}

Output:

Enter a binary number: 11011
Decimal value of 11011 is 27

Method 3: Using recursion

In this method, we will use recursion to convert the binary number to a decimal number.

Algorithm

  1. Create a function and pass the binary number as an argument.
  2. Check if the binary number is 0, if it is return 0.
  3. Otherwise, return the last digit of the binary number plus 2 times the function call with the binary number divided by 10.

Let's see the program.

#include <stdio.h>

// function to convert binary to decimal
int bin2dec(int bin){
    if (bin == 0){
        return 0;
    }
    else{
        return (bin % 10) + 2 * bin2dec(bin / 10);
    }
}

int main() {
    printf("Enter a binary number: ");
    int bin;
    scanf("%d", &bin);

    // convert binary to decimal
    int dec = bin2dec(bin);
    printf("Decimal value of %d is %d", bin, dec);
    return 0;
}
binary to decimal in c example 2
Binary to Decimal in C Example 2

Output:

Enter a binary number: 10011
Decimal value of 10011 is 19

Method 4: Using Bitwise Operators

In this method, we will use bitwise operators to convert the binary number to a decimal number.

Algorithm

  1. Create a function and pass the binary number as an argument.
  2. Declare a variable decimal and initialize it to 0.
  3. Create a loop that runs until the binary number is 0.
  4. Shift the decimal number to the left by 1.
  5. Check if the last digit of the binary number is 1, if it is add 1 to the decimal number.
  6. Divide the binary number by 10.
  7. Return the decimal number.

Let's see the program.

#include <stdio.h>

// function to convert binary to decimal
int bin2dec(int bin){
    int dec = 0;
    int i = 0;
    
    while (bin != 0){
        dec = dec << 1;
        if (bin % 10 == 1){
            dec += 1;
        }
        bin = bin / 10;
    }

    return dec;
}

int main() {
    printf("Enter a binary number: ");
    int bin;
    scanf("%d", &bin);

    // convert binary to decimal
    int dec = bin2dec(bin);
    printf("Decimal value of %d is %d", bin, dec);
    return 0;
}

Output:

Enter a binary number: 10110
Decimal value of 10110 is 22