C program to convert Binary to Decimal number system

Previous Program Next Program

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

Example

Input

Input number: 0011

Output

Decimal: 3

Required knowledge

Basic C programming, For loop, String

Must have programming knowledge for this program

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

Conversion from binary number system to decimal number system.
From the above binary to decimal conversion image. You can easily reduce logic of conversion. Decimal number is the result of sum of powers of 2 from right to left.

Below is the step by step descriptive logic to convert binary to decimal number system.

  1. Read binary number from user. Store it in some variable say binary.
  2. Extract the 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

/**
 * 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;
} 

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 number system 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 ;)

Recommended posts

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment