C program to convert Binary to Decimal number system

Write a C program to read 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.

Example:
Input binary number: 0011
Output decimal number: 3

Required knowledge:

Basic C programming, For loop, String

Binary number system

Binary number system is a number system that uses base 2 number system. It uses only two symbols to represent all number i.e. 0 and 1.

Algorithm:

Steps to convert from binary number system to decimal number system:
Step 1: Read binary number in binary.
Step 2: Set decimal = 0 and N = 0.
Step 3: Get Last digit of binary number and store in some variable i.e. rem = binary % 10.
Step 4: If rem == 1 then decimal = decimal + pow(2, N). (Where pow() is a function used to calculate power of any number.
Step 5: N = N + 1 and binary = binary / 10.
Step 6: Repeat Step 3-5 till binary > 0 .
Step 7: Print the value of decimal.
Conversion from binary number system to decimal number system.


Program:

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

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

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

    printf("\nBinary 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 ;)


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
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment