C program to convert number from Binary to Decimal

In this program we will read Binary Value of a Number and converts it into Decimal Number Susyemr System. This program is for Binary to Decimal Conversion in C.

The logic behind to implement this program - Access each digit from the Binary Number multiply the digit by the power of 2 (for first digits from right side multiply digit with 2^0, second digits 2^1 and so on), add the result and finally you will get Decimal value of given Binary Number. Here we will multiply with the power of base and base of Binary number is 2.

For more details Learn: Computer Number System and its conversions.

Binary to Decimal Conversion using C program

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

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

int main()
{
    char bin[32] = { 0 };
    int dec, i;
    int cnt; /*for power index*/

    printf("Enter binary value: ");
    gets(bin);

    cnt = 0;
    dec = 0;
    for (i = (strlen(bin) - 1); i >= 0; i--) {
        dec = dec + (bin[i] - 0x30) * pow((double)2, (double)cnt);
        cnt++;
    }

    printf("DECIMAL value is: %d", dec);

    return 0;
}

Output:

    Enter binary value: 1000100001
    DECIMAL value is: 545

C Number System Conversion Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.