C program to convert Binary to Decimal number system

Write a C program to input binary number from user and convert binary number to decimal number system. How to convert from binary number system to decimal number system in C programming. Logic to convert binary to decimal number system in C programming.

Example

Input

Input number: 0011

Output

Decimal: 3

Required knowledge

Basic C programming, If else, While loop, Functions

Binary number system

Binary number system is a base 2 number system. Binary number system uses only 2 symbols to represent all its numbers i.e. 0 and 1.

Logic to convert binary to decimal number system

Binary to decimal conversion

Step by step descriptive logic to convert binary to decimal number system.

  1. Input binary number from user. Store it in a variable say binary.
  2. Find last digit from binary by performing modulo division. Which is lastDigit = binary % 10.
  3. If lastDigit is 1. Then add power of 2 to the decimal result. Which is decimal += pow(2, N), where N is the position from right.

Program to convert binary to decimal number system

/**
 * C program to convert binary number system to decimal number system
 */

#include <stdio.h>
#include <math.h>

#define BASE 2

int main()
{
    long long binary, decimal=0, tempBinary;
    int N=0;

    printf("Enter any binary number: ");
    scanf("%lld", &binary);

    tempBinary = binary;

    while(tempBinary!=0)
    {
        /* If current binary digit is 1 */
        if(tempBinary % 10 == 1)
        {
            decimal += pow(BASE, N);
        }

        N++;
        tempBinary /= 10;
    }

    printf("Binary number = %lld\n", binary);
    printf("Decimal number= %lld", decimal);

    return 0;
}

In the above program I have used pow() function. pow() function is used to find power of a number.

Note: The above program will convert only up to 18 binary bits and will also not check for negative binary value. You can use below approach to check for negative value and also to convert binary number (greater than 18 bits) to decimal number.

/**
 * C program to convert binary to decimal number system
 */

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

#define SIZE 16 /* N-bit binary number */

void getTwosComplement(char *twosComp, const char *);

int main()
{
    char binary[SIZE + 1], tempBinary[SIZE + 1];
    int i, j, signBit = 0;
    long long decimal = 0;

    printf("Enter any %d bit binary value: ", SIZE);
    gets(binary);

    strcpy(tempBinary, binary);

    /*
     * If sign bit is on find two's complement of the binary number
     */
    if(binary[0] == '1')
    {
        signBit = 1;
        getTwosComplement(tempBinary, binary);
    }

    /*
     * Convert decimal to binary number
     */
    for(i=0; i<SIZE; i++)
    {
        if(tempBinary[i] == '1')
        {
            decimal += pow(2, (SIZE - (i+1)));
        }
    }

    if(signBit==1)
    {
        decimal *= -1;
    }

    printf("Binary value = %s\n", binary);
    printf("Decimal value = %lld", decimal);

    return 0;
}

/*
 * Gets the 2's complement of the binary value.
 */
void getTwosComplement(char * twosComp, const char * binary)
{
    char onesComp[SIZE + 1];
    int i, carry=1;

    /*
     * Finds 1's complement of the binary number
     */
    for(i=0; i<SIZE; i++)
    {
        if(binary[i]=='1')
        {
            onesComp[i] = '0';
        }
        else if(binary[i]=='0')
        {
            onesComp[i] = '1';
        }
    }
    onesComp[SIZE] = '\0';

    /*
     * Adds 1 to 1's complement of the binary number to get 2's complement
     */
    for(i=SIZE-1; i>=0; i--)
    {
        if(onesComp[i]=='1' && carry==1)
        {
            twosComp[i] = '0';
        }
        else if(onesComp[i]=='0' && carry==1)
        {
            twosComp[i] = '1';
            carry = 0;
        }
        else
        {
            twosComp[i] = onesComp[i];
        }
    }
    twosComp[SIZE] = '\0';
}

Output

Enter any 16 bit binary value: 0100000010001100
Binary value = 0100000010001100
Decimal value = 16524

Happy coding 😉